无法从datagridview获取值到文本框

问题描述:

问候!

在我的项目中,要求我如果单击datagridviewcell,它应该将值弹出到表单中的文本框中,实际上它有时会工作,有时不会工作,不知道我在做什么,这就像,在第三次点击它不工作。我使用的代码是:

Greetings!
In my project there is a requirement for me that if I click on datagridviewcell, it should popup the values into the text boxes in the form, actually it is working sometimes and sometimes not working, not knowing what the wrong I am doing, it is like, on the third click it is not working. The code which I am using is:

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();

}

好的,好像你的问题出在你声明的selectedcells部分。我曾多次遇到同样的问题。我通过以下方式解决了这个问题:



首先创建一个整数变量来存储你正在使用的行。

then找到CellMouseClick事件并从e.rowindex读取行



让我们假设你的变量叫做ri然后:



这样做

Ok, It seems like your problem is with the selectedcells portion of your statement. I have had the same issue numerous times. What I have done to resolve this as follow:

Firstly create an integer variable to store the row that you are working with.
then find the CellMouseClick event and read the row from the e.rowindex

lets assume your variable is called ri then :

do this
txtPrjNmae.Text = dataGridView2.Rows[ri].Cells[0].Value.ToString();

b $ b

希望有所帮助。



Hope it helps.


试试这个 -



私人void dataGridView2_CellContentClick(object sender,DataGridViewCellEventArgs e)

{

int i = dataGridView2.CurrentRow.Index;



txtPrjNmae.Text = dataGridView2 [0,i] .Value.ToString();

txtPrjdescription.Text = dataGridView2 [1,i] .Value.ToString();

txtPrjDate.Text = dataGridView2 [2,i] .Value.ToString();

txtPrjSize.Text = dataGridView2 [3, i] .Value.ToString();

txtPrjManager.Text = dataGridView2 [4,i] .Value.ToString();

}
Try This -

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i = dataGridView2.CurrentRow.Index;

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

>