Values of Selected Row from a Gridview in TextBox


Here in this Example you can learn how to get the values of selected row from a Grid view and display the values in textboxes using C# code. We can edit, update, cancel, select, and delete data also.

Default.aspx code

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

<style type="text/css">
        .style1
        {
            width: 112%;
        }
        .style2
        {
            width: 169px;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
    <div style="background-color: #FF9900; width: 356px;">

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="id" Height="264px" onrowediting="GridView1_RowEditing"
            onrowupdating="GridView1_RowUpdating"
            onselectedindexchanged="GridView1_SelectedIndexChanged" Width="379px"
            BackColor="#66CCFF" BorderColor="#FF0066"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowdeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="Id" />
                <asp:BoundField DataField="name" HeaderText="Name" />
                <asp:BoundField DataField="address" HeaderText="Address" />
                <asp:BoundField DataField="salary" HeaderText="Salary" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
 

            </Columns>
            <SelectedRowStyle BackColor="#FF66FF" />
        </asp:GridView>

        &nbsp;&nbsp;&nbsp;&nbsp;
        <table class="style1" style="background-color: #CCFF99; height: 268px;">
            <tr>
                <td class="style2">
                    ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Salary</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
            </tr>

        </table>

        <br />

        <br />

    </div>
    </form>
</body>
</
html>

Default.aspx.cs code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand();
    SqlConnection con;
    SqlDataAdapter sqlda;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
 
            binddata();

        }

    }
    public void binddata()
    {
        SqlConnection con = new SqlConnection(connStr);
        con.Open();
        sqlda = new SqlDataAdapter("select * from employee", con);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
        TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
        TextBox3.Text = GridView1.SelectedRow.Cells[2].Text;
        TextBox4.Text = GridView1.SelectedRow.Cells[3].Text;

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string sal = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        con = new SqlConnection(connStr);
        con.Open();
        cmd = new SqlCommand("Update employee set name='" + name + "',address='" + address + "',salary='" + sal + "' where id='" + s + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        binddata();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binddata();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(connStr);
        cmd.Connection = con;
        cmd.CommandText = "DELETE FROM employee WHERE id='" + GridView1.DataKeys[e.RowIndex].Values[0].ToString() + "'";
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        binddata();
    }
}


Output

GridView.gif