C# - Sort bound DataGridView - Child list cannot be created exception
-
Wednesday, July 16, 2008 5:38 AM
Hi,
After creating a DataTable and binding my DataGridView "dgvToSort" to it, I am trying to sort my DataGridView by columns "Orientation, Position asc", using the code below, but is not working.
I get Exception: {"Child list for field FieldsConfig cannot be created."} System.Exception {System.ArgumentException}
Thanks in advance for any help.// Sort bound DataGridView // Create DataSet based on default DataGridView. DataSet dataSet = (DataSet)dgvToSort.DataSource; // Create Dataview and set it to DefaultView of the DataSet. DataView dataView = dataSet.Tables[0].DefaultView; // Set Sort. dataView.Sort = "Orientation, Position asc"; // Set DataGridView to show sorted DataView. dgvToSort.DataSource = dataView; dgvToSort.DataMember = "FieldsConfig ";
The DataTable was bound to the DataGridView in this way:
// Create table and Bind DataGrid to DataSet, with FieldsConfig as the topmost DataTable. dgvPT.DataSource = CreateTable("FieldsConfig", originalDataGridView); dgvPT.DataMember = "FieldsConfig"; private DataSet CreateTable(string tableName, DataGridView dgv) { // Create DataTable. System.Data.DataTable table = new System.Data.DataTable(tableName); // Declare variables for DataColumn and DataRow objects. DataSet dataSet; // Create DataColumns and add them to DataTable. table.Columns.Add(CreateDataColumn("System.String", "Column", "Column", false, true, true)); table.Columns.Add(CreateDataColumn("System.String", "Caption", "Caption", false, false, false)); table.Columns.Add(CreateDataColumn("System.String", "Orientation", "Orientation", false, false, false)); table.Columns.Add(CreateDataColumn("System.Int32", "Position", "Position", false, false, false)); table.Columns.Add(CreateDataColumn("System.String", "Function", "Function", false, false, false)); // Instantiate the DataSet variable. dataSet = new DataSet(); // Add the new DataTable to the DataSet. dataSet.Tables.Add(table); // Create rows and add them to the DataTable. AddRowsToTable(dgv, table); // return DataSet. return dataSet; }
Everything is possible, impossible just takes longer
All Replies
-
Friday, July 18, 2008 8:02 AM
Discussion in Windows Forms Data Controls and Databinding
Please remember to mark the replies as answers if they help and unmark them if they provide no help.