How to display data from the selected rows in textbox

This post shows how to display values in the selected row in a Gridview in the textboxes below.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
           <div style="text-align: left">
               <asp:GridView ID="GridView1" runat="Server" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
           <div style="text-align: right; vertical-align: top">
               <asp:TextBox ID="txt1" runat="Server"></asp:TextBox><br />
               <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox><br />
               <asp:TextBox ID="TextBox2" runat="Server"></asp:TextBox><br />
               <asp:TextBox ID="TextBox3" runat="Server"></asp:TextBox><br />

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SelectRowInTextbox : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)



    private void BindGridViewView()
        if (Session["strTemp"] != null)

            GridView1.DataSource = Session["strTemp"] as DataTable;

            GridView1.DataSource = GetCustomMadeDataTable();
    public DataTable GetCustomMadeDataTable()
        //Create a new DataTable object
        System.Data.DataTable objDataTable = new System.Data.DataTable();
        //Create three columns with string as their type
        objDataTable.Columns.Add("ISBN", typeof(string));
        objDataTable.Columns.Add("Title", typeof(string));
        objDataTable.Columns.Add("Publisher", typeof(string));
        objDataTable.Columns.Add("Year", typeof(string));
        DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["ISBN"];
        objDataTable.PrimaryKey = dcPk;
        objDataTable.Columns["ISBN"].AutoIncrement = true;
        objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 1; i <= 10; i++)
            dr = objDataTable.NewRow();
            dr[1] = "Title" + i.ToString();
            dr[2] = "Publisher" + i.ToString();
            dr[3] = "200" + i.ToString();
        Session["strTemp"] = objDataTable;
        return objDataTable;

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        GridViewRow row = GridView1.SelectedRow;
        txt1.Text = row.Cells[1].Text;
        TextBox1.Text = row.Cells[2].Text;
        TextBox2.Text = row.Cells[3].Text;
        TextBox3.Text = row.Cells[4].Text;
Next Post Previous Post
No Comment
Add Comment
comment url