How to add a new row to datagridview programmatically

前端 未结 18 2029
滥情空心
滥情空心 2020-11-22 09:40

if add row to DataTable

DataRow row = datatable1.NewRow();
row[\"column2\"]=\"column2\";
row[\"column6\"]=\"column6\";
datatable1.Rows.Add(row);         


        
18条回答
  •  日久生厌
    2020-11-22 09:51

    If you´ve already defined a DataSource, 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();
    

提交回复
热议问题