打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# DataGridView动态添加新行的二个方法

方法1:常用的方法
 

复制代码 代码示例: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"。

方法2:
 

复制代码 代码示例: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);
方法2适用于某些特殊场合,例如,要在新行中的某些单元格添加下拉框、按钮之类的控件时。
解释:
DataGridViewRow row = new DataGridViewRow();
创建DataGridView的行对象,DataGridViewTextBoxCell是单元格的内容是个 TextBox,DataGridViewComboBoxCell是单元格的内容是下拉列表框,同理可知,DataGridViewButtonCell是单元格的内容是个按钮。

textboxcell是新创建的单元格的对象,可以为该对象添加其属性。
然后通过row.Cells.Add(textboxcell)为row对象添加textboxcell单元格。要添加其他的单元格,用同样的方法即可。
最后通过dataGridView1.Rows.Add(row)为dataGridView1控件添加新的行row。

以上就是c#中为datagridview动态添加新数据行的方法,希望对大家有所帮助。

关键词:
本文原始链接:http://www.jbxue.com/article/6709.html

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
关于DataGridView控件单元格实现下拉选择以及其他
c#winform中,对DataGridView数据进行操作,一次性保存
(转)使用DataGridView控件常见问题解答 - 边写边唱 - 博客园
C#学习教程:如何从DataGridView中的行获取DataRow分享
为生成的新行添加默认值
DataGridView中的Combobox的应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服