C# DataGridView添加新行的2个方法
2013-03-04 10:22
686 查看
可以静态绑定数据源,这样就自动为DataGridView控件添加 相应的行。假如需要动态为DataGridView控件添加新行,方法有很多种,下面简单介绍如何为DataGridView控件动态添加新行的两种方 法:
方法一:
int index=this.dataGridView1.Rows.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.Rows.Add()事件为DataGridView控件增加新的行,该函数返回添加新行的索引号,即新行的行号,然后可以通过该索引号操作该行的各个单元格,如dataGridView1.Rows[index].Cells[0].Value = "1"。这是很常用也是很简单的方法。
方法二:
DataGridViewRow row = new DataGridViewRow();
DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();
textboxcell.Value = "aaa";
row.Cells.Add(textboxcell);
DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
row.Cells.Add(comboxcell);
dataGridView1.Rows.Add(row);
方法二比方法一要复杂一些,但是在一些特殊场合非常实用,例如,要在新行中的某些单元格添加下拉框、按钮之类的控件时,该方法很有帮助。
DataGridViewRow row = new DataGridViewRow(); 是创建DataGridView的行对象,DataGridViewTextBoxCell是单元格的内容是个 TextBox,DataGridViewComboBoxCell是单元格的内容是下拉列表框,同理可知,DataGridViewButtonCell是单元格的内容是个按钮,等等。textboxcell是新创建的单元格的对象,可以为该对象添加其属性。然后通过row.Cells.Add(textboxcell)为row对象添加textboxcell单元格。要添加其他的单元格,用同样的方法即可。
最后通过dataGridView1.Rows.Add(row)为dataGridView1控件添加新的行row。
您可能感兴趣的文章:
相关文章推荐
- C# DataGridView添加新行的2个方法
- C# DataGridView添加新行的2个方法
- C# DataGridView添加新行的2个方法
- 从xib创建完,往xib里添加东西的 2个方法
- C# DataGridView控件 动态添加新行
- DataGridView动态添加新行的两种方法
- jQuery实现html表格动态添加新行的方法
- jQuery实现html表格动态添加新行的方法
- C# DataGridView控件 动态添加新行 .
- c# DataGridView动态添加新行的二个方法
- MSXML2;System.ServiceModel.Configuration;对应dll的添加方法
- php通过curl添加cookie伪造登陆抓取数据的方法
- 添加一个关闭ProgressDialog的静态方法:
- 防止复制的网页添加“零碎”的方法
- Android下添加状态栏图标的方法
- Linux CentOS 6.6 安装添加yum 源的简单处理方法
- ubuntu 添加字体的无敌快捷方法....
- JS动态添加方法并绑定参数
- veeam 6.5 添加vmware exsi 主机没输入用户名和密码框解决方法
- 在创建Vector对象的时候,添加了几个元素,然后使用get(int index)方法,结果显示出错了;