C# - Sort bound DataGridView - Child list cannot be created exception

質問 C# - Sort bound DataGridView - Child list cannot be created exception

  • Wednesday, July 16, 2008 5:38 AM
     
      Has Code

    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"falsetruetrue));  
      table.Columns.Add(CreateDataColumn("System.String""Caption""Caption"falsefalsefalse));  
      table.Columns.Add(CreateDataColumn("System.String""Orientation""Orientation"falsefalsefalse));  
      table.Columns.Add(CreateDataColumn("System.Int32""Position""Position"falsefalsefalse));  
      table.Columns.Add(CreateDataColumn("System.String""Function""Function"falsefalsefalse));  
     
      // 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
    • Edited by ajliaks Wednesday, July 16, 2008 5:52 AM more details
    • Moved by jack 321 Friday, July 18, 2008 8:02 AM off topic for C# language
    •  

All Replies