Rows cannot be programmatically added to the datagridview's row collection when the control is data-bound

后端 未结 6 940
囚心锁ツ
囚心锁ツ 2020-11-29 10:02

First of all, I looked up this related question in here but the solution dataGridView1.Rows.Add() doesn\'t work in my case.

In my Datagridview, I have

相关标签:
6条回答
  • 2020-11-29 10:38

    When I write this line in Form_Load

    DGVData.DataSource = DataSQLite.GetData("SELECT * from tableName");
    

    And trying to add a new row this error appeared to me

    Instead of using DataSource try this code

     private void Form_Load(object sender, EventArgs e) {
            DataSQLite.OpenDB();      // this line to open connection to Database and DataSQLite class i created it
            DataTable tbl = DataSQLite.GetData("SELECT * from tableName");
            for (int i = 0; i < tbl.Rows.Count; i++) {
                DGVData.Rows.Add();  // this line will create new blank row
                for (int j = 0; j < Numberofcolumns; j++) {
                    DGVData.Rows[i].Cells[j].Value = tbl.Rows[i][j].ToString();
                }
            }
        }
    

    After this code you can easily add rows

    0 讨论(0)
  • 2020-11-29 10:39

    You can get the DataGridView's DataSource and cast it as a DataTable.

    Then add a new DataRow and set the fields' values.

    Add the new row to the DataTable and Accept the changes.

    In C# it would be something like this:

    DataTable dataTable = (DataTable)dataGridView.DataSource;
    DataRow drToAdd = dataTable.NewRow();
    
    drToAdd["Field1"] = "Value1";
    drToAdd["Field2"] = "Value2";
    
    dataTable.Rows.Add(drToAdd);
    dataTable.AcceptChanges();
    
    0 讨论(0)
  • 2020-11-29 10:39

    After adding a new row, you have to set the row index in boundary of row count. You have to do these steps.

    1. First, add row in DataGridView:

      dataGridView1.Rows.Add();
      
    2. Second, set new row index to count - 1:

      int RowIndex = dataGridView1.RowCount - 1;
      
    3. Then at last, set the controls values in it:

      DataGridViewRow R = dataGridView1.Rows[RowIndex];
      R.Cells["YourName"].Value = tbName.Text;
      

    And if your datagrid's source is datattable you have to add row in that table.Give new values to the newly added row in data table and at last rebind the datagrid with updated datatable.

        DataRow row = dt.NewRow();  
        row["columnname"] = tbName.Text.toString();  
        dt.Rows.Add(row);
        dt.AcceptChanges();  
    
       dataGridView1.DataSource = dt;  
       dataGridView1.DataBind();
    

    Check if you have set the index of the new row properly. Perhaps that's why you are getting this error.

    0 讨论(0)
  • 2020-11-29 10:41

    The Bound Datagridview has a problem that when you want to add your data programmatically it prevent it to add it directly. so the indirect and best way to add data is like this.. and remember never add data directly to datagridview programmatically because it create problem always, add data to your datasource instead :-)

    code for VB.NET
    Dim r As DataRow ( C# : Datarow r=new Datarow() below codes apply to C# also)
    r = dataset.Tables(0).NewRow
    r.Item("field1") = "2"
    r.Item("field2") = "somevalue"
    dataset.Tables(0).Rows.Add(r)
    
    dataset.Tables(0).acceptchanges()
    
    the update will goes as you do ever
    
    0 讨论(0)
  • 2020-11-29 10:46

    It appears as though you are using the DataSource property of the DataGridView. When this property is used to bind to data you cannot explicitly add rows directly to the DataGridView. You must instead add rows directy to your data source.

    For example if your data source is a DataTable, using the DataTable that is assigned to the DataSource property (untested):

    private void AddARow(DataTable table)
    {
        // Use the NewRow method to create a DataRow with 
        // the table's schema.
        DataRow newRow = table.NewRow();
    
        // Add the row to the rows collection.
        table.Rows.Add(newRow);
    }
    
    0 讨论(0)
  • 2020-11-29 10:50

    The best solution I found:

    //create datatable and columns
    DataTable dtable = new DataTable();
    dtable.Columns.Add(new DataColumn("Column 1"));
    dtable.Columns.Add(new DataColumn("Column 2"));
    
    //simple way create object for rowvalues here i have given only 2 add as per your requirement
    object[] RowValues = { "", "" };
    
    //assign values into row object
    RowValues[0] = "your value 1";
    RowValues[1] = "your value 2";
    
    //create new data row
    DataRow dRow;
    dRow = dtable.Rows.Add(RowValues);
    dtable.AcceptChanges();
    
    //now bind datatable to gridview... 
    gridview.datasource=dtable;
    gridview.databind();
    

    Source: http://www.codeproject.com/Questions/615379/Adding-rows-to-datagridview-with-existing-columns

    0 讨论(0)
提交回复
热议问题