為DataTable新增一行很簡單,只要使用NewRow和Add方法。那麼,如何在DataGridView中實現同樣的操作呢?
要為DataGridView新增行,您可以複製現有行的結構,然後分別為其單元格賦值。
方法一
<code class="language-c#">DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = "单元格1的值"; row.Cells[1].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);</code>
方法二
如果您知道列名,可以直接在複製中引用它們。
<code class="language-c#">DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells["Column1"].Value = "单元格1的值"; row.Cells["Column2"].Value = "单元格2的值"; yourDataGridView.Rows.Add(row);</code>
Add方法
使用Add方法可以直接在參數中指定值。
<code class="language-c#">this.dataGridView1.Rows.Add("Column1 Value", "Column2 Value", "Column3 Value");</code>
Insert方法
Insert方法允許您在特定索引處新增一行。
<code class="language-c#">this.dataGridView1.Rows.Insert(0, "Value1", "Value2", "Value3");</code>
有關在DataGridView中操作行的更多詳細信息,請參閱MSDN文件:https://www.php.cn/link/8efa9015a4ef4632a954e820eca834ad
以上是如何在數據雜誌上編程添加一行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!