谁能告诉我这段代码有什么问题?
当我尝试将值写入文本框时,我得到一个错误:'当前行返回null在datagridview'这是一个例子,我只需要运行,但我不明白什么是错的。
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
textBox6.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[0].Value = textBox1.Text;////error
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[1].Value = textBox2.Text; //error
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[2].Value = textBox3.Text;//error
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[3].Value = textBox4.Text;//error
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[4].Value = textBox5.Text;//error
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
dataGridView1.CurrentRow.Cells[5].Value = textBox6.Text;//error
}
在设置CurrentRow值之前先设置CurrentRow。