javasocute
javasocute

Reputation: 648

How to populate textbox with value of gridview control?

With just a regular value you would write something like

  protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow email = GridView1.SelectedRow;
    txtbox.Text = email.Cells[5].Text;

}

However, I want to populate that textbox with a value that is a textbox control in the gridview. I have a list of rows and depending on which row I select that textbox will populate with that control value. Any help/advice would be greatly appreciated.

Upvotes: 2

Views: 9628

Answers (4)

srikanthbattula
srikanthbattula

Reputation: 41

You can find a better code here on how to populate gridview with the text box values of your form. http://sharepoint-2010-world.blogspot.in/2013/10/populating-grid-with-form-values.html

Upvotes: 0

James Johnson
James Johnson

Reputation: 46067

I would suggest using datakeys for this. It's a lot easier and a lot more reliable than using the cell index:

<asp:GridView ID="Gridview1" runat="server" DataKeyNames="Column1, Column2" ...>

Then, in the code-behind you can access the values like this:

protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow email = GridView1.SelectedRow;
    txtbox.Text = (string)GridView1.DataKeys[email.RowIndex]["Column1"];    
}

Upvotes: 1

Kamran Pervaiz
Kamran Pervaiz

Reputation: 1931

Try below code.

TextBox tb = GridView1.SelectedRow.FindControl("textboxId") as TextBox;
textbox.Text = tb.Text;

Upvotes: 1

coder
coder

Reputation: 13248

Try this:

GridView1.RowCommand += GridView1_RowCommand;
private void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(Convert.ToString(e.CommandArgument));
            GridViewRow row = GridView1.Rows[index];
            this.NameTextBox.Text = Server.HtmlDecode(row.Cells[1].Text);

        }

}

Upvotes: 1

Related Questions