How to add a new row to datagridview programmatically

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

    If you need to manipulate anything aside from the Cell Value string such as adding a Tag, try this:

    DataGridViewRow newRow = (DataGridViewRow)mappingDataGridView.RowTemplate.Clone();
    newRow.CreateCells(mappingDataGridView);
    
    newRow.Cells[0].Value = mapping.Key;
    newRow.Cells[1].Value = ((BusinessObject)mapping.Value).Name;
    newRow.Cells[1].Tag = mapping.Value;
    
    mappingDataGridView.Rows.Add(newRow);
    

提交回复
热议问题