Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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();

}
Posted
Updated 4-Sep-13 22:41pm
v2
Comments
Marvin Ma 5-Sep-13 4:48am    
You should solve this problem by using binding, if you are familiar with this.
So you could bind the text property one way to the selected item in your datagrid.
[no name] 5-Sep-13 5:05am    
"on the third click it is not working" does not mean anything to anyone but you. You have to tell us what you think "not working" means.
karthik reddy mereddy 5-Sep-13 5:12am    
If I select the datagridview cell, it should show the data in the textbox, for the first two times it is okay, but I select the datagridviewcell for the third time then it is not showing the data in the textbox.
[no name] 5-Sep-13 5:41am    
Then you need to run your code through the debugger and find out why. Maybe there nothing in the cells to display but we can't know that since you have not provided any detail.

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


Hope it helps.
 
Share this answer
 
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();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900