电脑桌面
添加小米粒文库到电脑桌面
安装后可以在桌面快捷访问

C中DataGridView控件使用大全

C中DataGridView控件使用大全_第1页
1/24
C中DataGridView控件使用大全_第2页
2/24
C中DataGridView控件使用大全_第3页
3/24
DataGridView 动态添加新行: DataGridView 控件在实际应用中非常实用,特别需要表格显示数据时。可以静态绑定数据源,这样就自动为 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。 DataGridView 取得或者修改当前单元格的内容: 当前单元格指的是 DataGridView 焦点所在的单元格,它可以通过 DataGridView 对象的 CurrentCell 属性取得。如果当前单元格不存在的时候,返回 Nothing(C#是null) // 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 ...

1、当您付费下载文档后,您只拥有了使用权限,并不意味着购买了版权,文档只能用于自身使用,不得用于其他商业用途(如 [转卖]进行直接盈利或[编辑后售卖]进行间接盈利)。
2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。
3、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。

碎片内容

C中DataGridView控件使用大全

确认删除?
VIP
微信客服
  • 扫码咨询
会员Q群
  • 会员专属群点击这里加入QQ群
客服邮箱
回到顶部