Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i want to assign gridview row value to textbox.

What I have tried:

Here using the row index i am assign the textbox like this
for (int i = 0; i < gridview1.Rows.Count; i++)
                {
                    txtmytext.text = gridview1.Rows[i].Cells[1].Text;
                }

no any issue with this but if i added any extra column then the column index changed so i want to assign the textbox using the header text
something like this

(int i = 0; i < gridview1.Rows.Count; i++)
                {
                    txtmytext.text = gridview1.Rows[i].Headertext["MyColumn"].Text;
                }
Posted
Updated 25-Oct-17 20:51pm

1 solution

try this

string columnName = "YourColumnName";
            int index = 0;
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                if (GridView1.Columns[i].HeaderText == columnName)
                { index = i; break; }

            }
            txtmytext.text = GridView1.Rows[i].Cells[index].Text;


There is no pointing of iterating the rows of the gridview to assign the value to the textbox, however it will assign the last index value to it. Please correct it
 
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