datagridview实现手动添加行数据

 更新时间:2022年04月19日 11:40:26   作者:simpleshao  
这篇文章主要介绍了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";

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

最新评论