Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
25
FieldLayout errors when binding to DataRows
posted

There is an issue: I sometimes add/remove columns of my datatable. Now when I bind Grid directly to DataTable, it changes FieldLayout properly and defines new columns. But if I bind to any list of DataRows from this table, FieldLayout becomes corrupted and I get following binding error:

GetCellValue failed, exception type: System.ArgumentException, exception msg: Column 'C3' does not belong to table DataTable1., item index: 8, DataItem: DynamicGridBug.DataSet1+DataTable1Row

I've attached the sample project. If you click 'source 1', 'source 2', 'source 1', you will see that C3 column is empty are there are lots of binding errors.

The only way to overcome this is to create new DataTable each time, and then it seems to work.

Any ideas?


private void Button_Click(object sender, RoutedEventArgs e)

        {

            grid.DataSource = null;

            DS.DataTable1.Rows.Clear();

            DS.DataTable1.Columns.RemoveAt(3);

            DS.DataTable1.Columns.Add("C3");

            for (int i = 0; i < 10; i++)

                DS.DataTable1.Rows.Add(new object[] { i, "1", "2", "3" });

            grid.FieldLayouts.Clear();

            grid.DataSource = new List<DataRow>((from r in DS.DataTable1 select r).ToList()); // bad

            // grid.DataSource = DS.DataTable1;  // good

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)

        {

            grid.DataSource = null;

            DS.DataTable1.Rows.Clear();

            DS.DataTable1.Columns.RemoveAt(3);

            DS.DataTable1.Columns.Add("C4");

            for (int i = 0; i < 10; i++)

                DS.DataTable1.Rows.Add(new object[] { i, "1", "2", "4" });

            grid.FieldLayouts.Clear();

            grid.DataSource = DS.DataTable1; // good

            // grid.DataSource = new BindingList<DataSet1.DataTable1Row>((from r in DS.DataTable1 select r).ToList()); // bad

        }

DynamicGridBug.zip
Parents Reply Children
No Data