向DataTable添加行很简单,只需使用NewRow和Add方法。那么,如何在DataGridView中实现同样的操作呢?
要向DataGridView添加新行,您可以克隆现有行的结构,然后分别为其单元格赋值。
方法一
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = "单元格1的值"; row.Cells[1].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);
方法二
如果您知道列名,可以直接在克隆中引用它们。
DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells["Column1"].Value = "单元格1的值"; row.Cells["Column2"].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);
Add方法
使用Add方法可以直接在参数中指定值。
this.dataGridView1.Rows.Add("Column1 Value", "Column2 Value", "Column3 Value");
Insert方法
Insert方法允许您在特定索引处添加一行。
this.dataGridView1.Rows.Insert(0, "Value1", "Value2", "Value3");
有关在DataGridView中操作行的更多详细信息,请参阅MSDN文档:https://www.php.cn/link/8efa9015a4ef4632a954e820eca834ad
以上是如何在数据杂志上编程添加一行?的详细内容。更多信息请关注PHP中文网其他相关文章!