locked
How to Display GridView Selected Row data in TextBox in Asp.Net |C#? RRS feed

  • Question

  • User-2071692902 posted

    Hi folks,

    I have a Gridview1 with three columns and three textboxs.. And I want to  Display GridView Selected Row data in TextBoxs when user clicks on one of the gridview rows.

    Thanks

    Saturday, March 23, 2019 10:32 PM

Answers

  • User-2071692902 posted

    Thanks for any one try to help.I found the solution. It is as follow:

    First: Change the "AutoGenerateSelectButton" to "true" from Properties. Then write the following code under  "GridView1_SelectedIndexChanging" event on aspx.cs

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
            TextBox1 .Text = row.Cells[1].Text;
            TextBox2.Text = row.Cells[2].Text;
            TextBox3.Text = row.Cells[3].Text;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 2, 2019 12:40 AM

All replies

  • User36583972 posted

    Hi folks,

    I have a Gridview1 with three columns and three textboxs.. And I want to  Display GridView Selected Row data in TextBoxs when user clicks on one of the gridview rows.

    Thanks

    I made a sample on my side. The following sample for your reference.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox1"   runat="server"   AutoPostBack="true"  Text='<%# Bind("Name") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Country">
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "John Hammond", "United States");
                    dt.Rows.Add(2, "Mudassar Khan", "India");
                    dt.Rows.Add(3, "Suzanne Mathews", "France");
                    dt.Rows.Add(4, "Robert Schidner", "Russia");
                    dt.Rows.Add(5, "Robert Schidner", "Russia");
                    ViewState["dt"] = dt;
                    this.BindGrid();
                }
            }
    
    
            protected void BindGrid()
            {
                GridView1.DataSource = ViewState["dt"] as DataTable;
                GridView1.DataBind();
            }
    
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRow row = GridView1.SelectedRow;
                int index = GridView1.SelectedRow.RowIndex;
                TextBox lblValuename = (TextBox)row.FindControl("TextBox1");
                TextBox lblValueCountry = (TextBox)row.FindControl("TextBox2");
                string message = "Row Index: " + index + "\\nName: " + lblValuename.Text + "\\nCountry: " + lblValueCountry.Text;
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
                    e.Row.Attributes["style"] = "cursor:pointer";
                }
            }
    

    Besides, the following article for your reference.

    Add Row Click event to GridView Rows in ASP.Net
    https://www.aspsnippets.com/Articles/Add-Row-Click-event-to-GridView-Rows-in-ASPNet.aspx

    Best Regards

    Yong Lu

    Monday, March 25, 2019 5:59 AM
  • User-2071692902 posted

    Thanks for any one try to help.I found the solution. It is as follow:

    First: Change the "AutoGenerateSelectButton" to "true" from Properties. Then write the following code under  "GridView1_SelectedIndexChanging" event on aspx.cs

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
            TextBox1 .Text = row.Cells[1].Text;
            TextBox2.Text = row.Cells[2].Text;
            TextBox3.Text = row.Cells[3].Text;
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 2, 2019 12:40 AM