What cause this DataTable.ImportRow()'s failure?

Chenbinhao 21 Reputation points
2020-10-01T19:52:11.45+00:00
 for (int i = 0; i < P5.Rows.Count; i++)  
        {  
            var dr = P5.Rows[i];  (※※※※)  
            if ((bool)dr[0])  
            {  
                dt.ImportRow(dr);  
            }  
        }  

I paused the program at line(※※※※), then executed importRow in the watch screen. Strange things happened: only the bool is different from the source!

dPQjp.png

===========
first added testing code===========

            DataTable original;  
            DataTable filtered;  
            private void button2_Click(object sender, EventArgs e)  
            {  
                original = new DataTable();  
                original.Columns.Add("bool", typeof(bool));  
                original.Columns.Add("column1", typeof(string));  
      
                for(int i=0; i<5; i++)  
                {  
                    DataRow dr = original.NewRow();  
                    dr[0] = true;  
                    dr[1] = i.ToString();  
                    original.Rows.Add(dr);  
                }  
                dataGridView1.DataSource = original;  
            }  
      
            private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
            {  
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);  
                Filter();  
                dataGridView2.DataSource = filtered;  
            }  
      
 void Filter()  
        {  
            DataTable dt = original.Clone();  
            for (int i = 0; i < original.Rows.Count; i++)  
            {  
                var dr = original.Rows[i];  
  
                if ((bool)dr[0])  
                {  
                    dt.ImportRow(dr);  
                }  
            }  
            filtered = dt;  
        }  
  
  

29845-qq%E6%88%AA%E5%9B%BE20201002151143.png

Demo
https://1drv.ms/u/s!AuN7KXeC80B9mVpNf88ji14QEuR0?e=Gts4Ls

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,884 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2020-10-02T19:58:56.263+00:00

    Try this code:

    private void dataGridView1_CellContentClick( object sender, DataGridViewCellEventArgs e )
    {
        dataGridView1.CommitEdit( DataGridViewDataErrorContexts.Commit );
        dataGridView1.BindingContext[dataGridView1.DataSource].EndCurrentEdit( );
        Filter( );
        dataGridView2.DataSource = filtered;
    }
    

1 additional answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2020-10-02T04:16:36.327+00:00

    Hi Chenbinhao,
    Based on your description, I made a test with the following code and it worked fine.
    So could you provide more related code to reproduce the situation.
    Here is my test code:

    DataTable t = new DataTable();  
    DataTable dt = new DataTable();  
    private void Form1_Load(object sender, EventArgs e)  
    {  
               
        t.Columns.Add(new DataColumn("Col1", typeof(bool)));  
        t.Columns.Add(new DataColumn("Col2", typeof(int)));  
        t.Columns.Add(new DataColumn("Col3", typeof(int)));  
        dt.Columns.Add(new DataColumn("Col1", typeof(bool)));  
        dt.Columns.Add(new DataColumn("Col2", typeof(int)));  
        dt.Columns.Add(new DataColumn("Col3", typeof(int)));  
        t.Rows.Add(true, 1, 1);  
        t.Rows.Add(false, 2, 2);  
      
    }  
      
    private void button1_Click(object sender, EventArgs e)  
    {  
        for (int i = 0; i < t.Rows.Count; i++)  
        {  
            var dr = t.Rows[i];  
            if ((bool)dr[0])  
        {  
            dt.ImportRow(dr);  
        }  
    }  
    }  
    

    The watch screen:
    29826-102.png
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.