这篇文章主要介绍了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#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- 实例详解C#实现http不同方法的请求 2022-12-26
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#中的Linq to JSON操作详解 2023-06-08
- Unity shader实现高斯模糊效果 2023-01-16
- C# 解析XML和反序列化的示例 2023-04-14
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16