Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi sir;

i want to edit only selected rows in datagridview.

but
in my case only first row is edited i want that whenever
i select any row and click on edit button then particular
row data is displayed in textbox.
below is my code:

C#
Bind();
 int i;

i = dataGridView1.SelectedCells[0].RowIndex;
                txtto.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
                txtsubject.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
                txtfrom.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
                txtdisp.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
                txtdepthide.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
 dateTimePicker1.Value =Convert.ToDateTime(dataGridView1.Rows[i].Cells[7].Value.ToString());
               txtrefer.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();

please help me your answer really helpful for me.

thanks in advance!
Posted
Updated 23-Apr-14 19:08pm
v2
Comments
Meysam Toluie 24-Apr-14 0:17am    
And where is the code you set updated row in dataGridView?
Laiju k 24-Apr-14 0:59am    
void GridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = GridView.SelectedRow;
}
you can use your code in this event

1 solution

Try using CurrentRow instead of SelectedCells.
i = dataGridView1.CurrentRow.Index; 


Or directly use it...
txtto.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
 
Share this answer
 
v2

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