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

后端 未结 6 958
囚心锁ツ
囚心锁ツ 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: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

提交回复
热议问题