Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display data in textbox when i click edit button (selected row data from datagridview )
ERROR:-Index was out of range. Must be non-negative and less than the size of the collection.
My following code:-

C#
private void button1_Click(object sender, EventArgs e)
       {
           int i;
           i = dataGridView2.SelectedCells[0].RowIndex;
           textBox10.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
           textBox11.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
           textBox12.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
           textBox13.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
           textBox14.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
           textBox15.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
           textBox16.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
           textBox17.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
           textBox18.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();
           textBox19.Text = dataGridView1.Rows[i].Cells[10].Value.ToString();
           textBox20.Text = dataGridView1.Rows[i].Cells[11].Value.ToString();
           textBox21.Text = dataGridView1.Rows[i].Cells[12].Value.ToString();

       }
Posted
Updated 26-Aug-14 23:20pm
v2
Comments
David Lee 145 27-Aug-14 5:21am    
How many cells do you have? Check the count of your cells.
Nishant.Chauhan80 27-Aug-14 5:28am    
in my table total cells 13 but i want dont show id column
Pheonyx 27-Aug-14 5:22am    
You are assuming that selected cells has something in it.
I would check that selectecells contains something before you try and read the row index.
Also, the Cells colleciton is a 0 based index so just double check you are reading the right cells.
Kumarbs 27-Aug-14 5:23am    
Make sure the no of cells are 13 as you used Cells[12].
Nishant.Chauhan80 27-Aug-14 5:30am    
yes sir but i want dont show id cell

1 solution

you are selecting rows form dataGridview2 and assigning values from dataGridview1 to textboxes.So please cross check rows and columns on both dataGridview1 and dataGridview2
 
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