这篇文章主要介绍了datagridview实现手动添加行数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
datagridview手动添加行数据
我在做软件模型界面时,通过功能按钮触发显示的datagridview中,为了方便,需要一些数据,仅写死数据就可以了,因此,不需要连接数据表,直接添加行就可以了。
代码如下:
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "1";
this.dataGridView1.Rows[index].Cells[1].Value = "11";
this.dataGridView1.Rows[index].Cells[2].Value = "1111";
this.dataGridView1.Rows[index].Cells[3].Value = "11111";
this.dataGridView1.Rows[index].Cells[4].Value = "111111";
this.dataGridView1.Rows[index].Cells[5].Value = "1111111";
this.dataGridView1.Rows[index].Cells[6].Value = "*-*";datagridview添加行的几种方式
1、数据绑定
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = customersDataSet;这样就会自动产生对应数据的行数据了。如果不自动产生列,则需手动添加列,把列的数据源属性名(DataPropertyName)设置为对应数据类的属性名。
2、手动添加
songsDataGridView.ColumnCount = 5;
....
songsDataGridView.Columns[0].Name = "Release Date";
....
string[] row0 = { "11/22/1968", "29", "Revolution 9",
"Beatles", "The Beatles [White Album]" };
songsDataGridView.Rows.Add(row0);另外,访问行单元格的方式可以这样
this.dataGridView1.Rows[1].Cells[0].Value = "new value";
//或者,等价的
this.dataGridView1[0, 1].Value = "new value";以上为个人经验,希望能给大家一个参考,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:datagridview实现手动添加行数据
基础教程推荐
猜你喜欢
- C#使用Chart绘制曲线 2023-05-22
- C#使用SQL DataAdapter数据适配代码实例 2023-01-06
- 浅谈C# 构造方法(函数) 2023-03-03
- 如何用C#创建用户自定义异常浅析 2023-04-21
- C#实现归并排序 2023-05-31
- C#使用NPOI将excel导入到list的方法 2023-05-22
- Unity虚拟摇杆的实现方法 2023-02-16
- C#中参数的传递方式详解 2023-06-27
- C#执行EXE文件与输出消息的提取操作 2023-04-14
- C# TreeView从数据库绑定数据的示例 2023-04-09
