Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
I'm building an application and i am building an update form.
I put some textboxes for the fields of the table which i wanna update.
I need when i click a row ,the textboxes retrieve data from each cell on the row ordered.

I wrote this code

C#
//this is my textboxes that will retrieve data
textBox5.Text = "";
textBox6.Text = "";
textBox8.Text = "";
textBox7.Text = "";
//this is the code of mine it works only when i select a row from the datagridview i need it work when i select a cell in this row
string[] arr = new string[4];
int i = 0;
foreach (DataGridViewCell item in dataGridView2.SelectedCells)
{
    arr[i] = item.Value.ToString();
    i++;
}
textBox5.Text = arr[0].ToString();
textBox6.Text = arr[1].ToString();
textBox8.Text = arr[2].ToString();
textBox7.Text = arr[3].ToString();


Please help me!

Thanks in Advance.
Posted
Updated 6-Jun-11 18:56pm
v3
Comments
CS2011 6-Jun-11 23:54pm    
Is it related to windows forms

Try following


C#
foreach (DataGridViewCell item in dataGridView2.CurrentRow.Cells)
{
    arr[i] = item.Value.ToString();
    i++;
}
 
Share this answer
 
Comments
A7mad Hegazy 7-Jun-11 16:26pm    
thank u a lot
If you are trying with WinForms then read further else let me know:
Whether you need to do this job on cell click (as mentioned in the code comments) or on the row select, you have events for these (RowEnter, CellEnter etc.). And the event args (DataGridViewCellEventArgs) provide you with RowIndex and ColumnIndex which you can use to get what you want. It's pretty straight forward, not sure what you are stuck with.
 
Share this answer
 
Comments
A7mad Hegazy 7-Jun-11 16:29pm    
thnx , i will do it in RowEnter that u mentioned
Hope this[^] article might help you.
 
Share this answer
 
Comments
A7mad Hegazy 7-Jun-11 16:27pm    
I will see it
it is very useful
thnx a lot

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