DataGridView动态添加新行:DataGridView 控件在实际应用中非常实用,特别需要表格显示数据时
可以静态绑定数据源,这样就自动为DataGridView 控件添加相应的行
假如需要动态为DataGridView 控件添加新行,方法有很多种,下面简单介绍如何为DataGridView 控件动态添加新行的两种方法:方法一:int index=this
dataGridView1
Add(); this
dataGridView1
Rows[index]
Cells[0]
Value = "1"; this
dataGridView1
Rows[index]
Cells[1]
Value = "2"; this
dataGridView1
Rows[index]
Cells[2]
Value = "监听 "; 利用 dataGridView1
Add()事件为 DataGridView 控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如 dataGridView1
Rows[index]
Cells[0]
Value = "1"
这是很常用也是很简单的方法
方法二:DataGridViewRow row = new DataGridViewRow(); DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell(); textboxcell
Value = "aaa"; row
Add(textboxcell); DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell(); row
Add(comboxcell); d