How to add a new row to datagridview programmatically

前端 未结 18 1761
滥情空心
滥情空心 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:50

    This is how I add a row if the dgrview is empty: (myDataGridView has two columns in my example)

    DataGridViewRow row = new DataGridViewRow();
    row.CreateCells(myDataGridView);
    
    row.Cells[0].Value = "some value";
    row.Cells[1].Value = "next columns value";
    
    myDataGridView.Rows.Add(row);
    

    According to docs: "CreateCells() clears the existing cells and sets their template according to the supplied DataGridView template".

提交回复
热议问题