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 = "";
}
}