DataGridView 访问任意行不崩溃

DataGridView行添加与访问技巧
本文探讨了在C#中使用DataGridView控件时,如何通过预先添加行来避免访问不存在行时的崩溃问题。介绍了在Form加载时通过循环添加9行,并在不同位置赋值的代码实现,展示了DataGridView的灵活使用方法。

int index= this.dataGridView1.rows.Add(); 先执行这行代码,然后访问任意行,不崩溃,

赋值不存在的行,只是不显示,或者无值。

什么原理呢?

 

 

 

 

 

 

 

一些其他

private void Form1_Load(object sender, EventArgs e)
{
add_9();

dataGridView1.Rows[0].Cells[3].Value = "1行3列";
dataGridView1.Rows[7].Cells[3].Value = "7行4列";
dataGridView1.Rows[8].Cells[2].Value = "8行4列";
dataGridView1.Rows[9].Cells[3].Value = "8行5列";
dataGridView1.Rows[7].Cells[1].Value = "7行4列";
dataGridView1.Rows[9].Cells[2].Value = "9行3列";
dataGridView1.Rows[6].Cells[3].Value = "6行4列";
}

void add_9()
{
for (int i = 0; i < 9; i++)
{

int index = this.dataGridView1.Rows.Add();
dataGridView1.Rows[index].Cells[0].Value = "";
dataGridView1.Rows[index].Cells[1].Value = "";
dataGridView1.Rows[index].Cells[2].Value = "";
dataGridView1.Rows[index].Cells[3].Value = "";


}

}

转载于:https://www.cnblogs.com/netdbo/p/9950433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值