Adding rows to a DataTable is simple, just use the NewRow and Add methods. So, how to achieve the same operation in DataGridView?
To add a new row to the DataGridView, you can clone the existing row's structure and assign values to its cells individually.
Method 1
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = "单元格1的值"; row.Cells[1].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);
Method 2
If you know the column names, you can reference them directly in the clone.
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells["Column1"].Value = "单元格1的值"; row.Cells["Column2"].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);
Add method
Use the Add method to specify values directly in parameters.
this.dataGridView1.Rows.Add("Column1 Value", "Column2 Value", "Column3 Value");
Insert method
The Insert method allows you to add a row at a specific index.
this.dataGridView1.Rows.Insert(0, "Value1", "Value2", "Value3");
For more details on manipulating rows in DataGridView, please refer to the MSDN documentation: https://www.php.cn/link/8efa9015a4ef4632a954e820eca834ad
The above is the detailed content of How to Programmatically Add a Row to a DataGridView?. For more information, please follow other related articles on the PHP Chinese website!