Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just print print the values in the grid view from database in need to print the value of a particular cell in the text box on the button click event.I just applied the following gives no error.But the value is not displaying in the textbox.Could you tell why is this happening?
GridView1.SelectedIndex = 0;
        TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;

There is Index Selection Problem, You Should be write your code this way, I think This will be helpfull for you.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "cmdBind")
LinkButton lb = (LinkButton)e.CommandSource;
int index = Convert.ToInt32(lb.CommandArgument);

TextBox3.Text = GridView1.Rows[index].Cells[2].Text.ToString();

TextBox4.Text = GridView1.Rows[index].Cells[3].Text.ToString();
txtplantname.Text = GridView1.Rows[index].Cells[4].Text;
TextBox1.Text = GridView1.Rows[index].Cells[5].Text;
Label1.Text = GridView1.Rows[index].Cells[6].Text;
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;
Designing Code
<asp:gridview id="GridView1" runat="server" bordercolor="#8080FF" width="100%" onrowcommand="GridView1_RowCommand" onpageindexchanging="GridView1_PageIndexChanging" onselectedindexchanged="GridView1_SelectedIndexChanged" xmlns:asp="#unknown">
<columns><asp:templatefield headertext="Details">
<asp:linkbutton id="lnkDet" commandname="cmdBind" commandargument="<%# ((GridViewRow) Container).RowIndex %>" runat="server" causesvalidation="false">View Details

Share this answer
Your check your cells.First you check how many cells in your grid.
i will give you sample. I have one gridview and textbox.

ArrayList arr = new ArrayList();

            GridView1.DataSource = arr;
            GridView1.SelectedIndex = 0;
            TextBox1.Text = GridView1.SelectedRow.Cells[0].Text.Trim();

You check like this.

Hope be helpful,
Theingi Win.
Share this answer
sreenathpktr 6-Oct-11 9:17am    
thank you very much..i fixed it.

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