无法从DataGridView获取值到文本框

3

你好!
我的项目中有一个要求,就是如果我点击datagridviewcell,它应该将值弹出到表单中的文本框中。实际上,有时它能够工作,有时它不能工作,我不知道我做错了什么,就像第三次点击时它就无法工作了。我使用的代码如下:

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int i = dataGridView2.SelectedCells[0].RowIndex;

    txtPrjNmae.Text = dataGridView2.Rows[i].Cells[0].Value.ToString();
    txtPrjdescription.Text = dataGridView2.Rows[i].Cells[1].Value.ToString();
    txtPrjDate.Text = dataGridView2.Rows[i].Cells[2].Value.ToString();
    txtPrjSize.Text = dataGridView2.Rows[i].Cells[3].Value.ToString();
    txtPrjManager.Text = dataGridView2.Rows[i].Cells[4].Value.ToString();           
}

2
为什么不使用 e.RowIndex 而是使用 dataGridView2.SelectedCells[0].RowIndex - Manish Mishra
1个回答

1
设置断点以进行检查和设置。
int i = e.RowIndex;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接