How to add a new row to datagridview programmatically

前端 未结 18 2144
滥情空心
滥情空心 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 10:01

    Adding a new row in a DGV with no rows with Add() raises SelectionChanged event before you can insert any data (or bind an object in Tag property).

    Create a clone row from RowTemplate is safer imho:

    //assuming that you created columns (via code or designer) in myDGV
    DataGridViewRow row = (DataGridViewRow) myDGV.RowTemplate.Clone();
    row.CreateCells(myDGV, "cell1", "cell2", "cell3");
    
    myDGV.Rows.Add(row);
    

提交回复
热议问题