Display The Selected Row Data Of GridView In TextBoxes

This example will help you learn how to display the selected row data of GridView into the TextBoxes using 3-Tier Architecture in ASP.NET. 

Step 1
Open Visual Studio and select "ASP.NET Empty Website template". Select the path where you want to create the project.
 
Display The Selected Row Data Of GridView In TextBoxes

Step 2

After selecting ASP.NET Empty Web Site template, add a new "class library" project with the name “DAL” in solution explorer.

Display The Selected Row Data Of GridView In TextBoxes
 
Display The Selected Row Data Of GridView In TextBoxes

Step 3

After adding the new "class library" project with the name “DAL”, write the following code.

Namespaces

  1. using System.Data;  
  2. using System.Data.SqlClient;   
code

  1. publicclassclsDAL {  
  2.     SqlConnection con = new SqlConnection("Data Source=DESKTOP-LDDD5B0;Initial Catalog=3TireDB;Integrated Security=True");  
  3.     publicvoid InsetData(string _name, string _email, string _address) {  
  4.         con.Open();  
  5.         SqlDataAdapter adpt = new SqlDataAdapter("Insert into Usertbl(Name,Email,Address) values('" + _name + "','" + _email + "','" + _address + "')", con);  
  6.         DataTable dt = new DataTable();  
  7.         adpt.Fill(dt);  
  8.         con.Close();  
  9.     }  
  10.     publicvoid DeletData(Int32 _ID) {  
  11.         con.Open();  
  12.         SqlDataAdapter adpt = new SqlDataAdapter("Delete from Usertbl where ID= " + _ID + "", con);  
  13.         DataTable dt = new DataTable();  
  14.         adpt.Fill(dt);  
  15.         con.Close();  
  16.     }  
  17.     publicobject SelectData() {  
  18.         DataTable dt = new DataTable();  
  19.         SqlDataAdapter adpt = new SqlDataAdapter("Select * from Usertbl ", con);  
  20.         adpt.Fill(dt);  
  21.         return dt;  
  22.     }  
  23.     publicvoid hfData(Int32 _hfd) {  
  24.         DataTable dt = new DataTable();  
  25.         if (dt.Rows.Count > 0) {  
  26.             SqlDataAdapter adpt = new SqlDataAdapter("Select (ID) from Usertbl values(" + _hfd + ")", con);  
  27.             adpt.Fill(dt);  
  28.         }  
  29.     }  
  30.     publicvoid UpdData(string _name, string _email, string _address, Int32 _Id) {  
  31.         con.Open();  
  32.         SqlDataAdapter adpt = new SqlDataAdapter("Update Usertbl set [Name]='" + _name + "',[Email]='" + _email + "',[Address]='" + _address + "' where [ID]=" + _Id + "", con);  
  33.         DataTable dt = new DataTable();  
  34.         adpt.Fill(dt);  
  35.         con.Close();  
  36.     }  
  37. }  

Step 4

Now, add one more “Class Libray ” with the name “BLL” to the solution explorer to implement Business Logic Layer.

Step 5

After adding BLL, add the reference of “DAL” to it.

Step 6

After adding the reference to BLL, write the following code.

Namespaces

using DAL;

code

  1. publicclasscslBLL {  
  2.     clsDAL DALobj = newclsDAL();  
  3.     publicvoid Insertvalues(string _Nam, string _email, string _address) {  
  4.         DALobj.InsetData(_Nam, _email, _address);  
  5.     }  
  6.     publicvoid Deletevalues(Int32 _id) {  
  7.         DALobj.DeletData(_id);  
  8.     }  
  9.     publicobject SelectValues() {  
  10.         return DALobj.SelectData();  
  11.     }  
  12.     publicvoid hfDataID(Int32 _id) {  
  13.         DALobj.hfData(_id);  
  14.     }  
  15.     publicvoid UpdateValues(string _name, string _email, string _address, Int32 _Id) {  
  16.         DALobj.UpdData(_name, _email, _address, _Id);  
  17.     }  
  18. }  

Step 5

Now, add a new web form with the name ”Index.aspx” to your solution explorer’s Empty website templete.

Step 6

Now, design your “Index.asp” page using the following code.

Index.aspx

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Indext.aspx.cs"Inherits="Indext"%>  
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <htmlxmlns="http://www.w3.org/1999/xhtml"> <headid="Head1" runat="server">  
  4.     <title></title>  
  5.     <styletype="text/css"> .style1 { width: 71%; } .style2 { font-size: large; color: #000046; } </style> </head> <body>  
  6.         <formid="form1"runat="server">  
  7.             <tablealign="center"class="style1">  
  8.                 <tr>  
  9.                     <td>                                                                                       <spanclass="style2"><strong>3 - Tire Architecture</strong></span>  </td>  
  10.                 </tr>  
  11.                 <tr>  
  12.                     <td>  
  13.                         <asp:HiddenFieldID="HiddenField1"runat="server" />  
  14.                     </td>  
  15.                     <td>   </td>  
  16.                     <td>   </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td>                                                                       <asp:LabelID="Label1"runat="server"Text="Name">  
  20.                             </asp:Label>   <asp:TextBoxID="TextBox1"runat="server"Width="222px"Style="margin-left: 5px">  
  21.                                 </asp:TextBox>    <asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ErrorMessage="Name is Reqired" ControlToValidate="TextBox1" ForeColor="Red">  
  22.                                     </asp:RequiredFieldValidator>  
  23.                     </td>  
  24.                 </tr>  
  25.                 <tr>  
  26.                     <td>                                                                    <asp:LabelID="Label2"runat="server"Text="Emmail">  
  27.                             </asp:Label>    <asp:TextBoxID="TextBox2"runat="server"Width="222px"Style="margin-left: 0px">  
  28.                                 </asp:TextBox>    <asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ErrorMessage="Email is Required" ControlToValidate="TextBox2" ForeColor="Red">  
  29.                                     </asp:RequiredFieldValidator>  
  30.                     </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td>                                                                    <asp:LabelID="Label3"runat="server"Text="Address">  
  34.                             </asp:Label>    <asp:TextBoxID="TextBox3"runat="server"Width="222px"Style="margin-left: 0px">  
  35.                                 </asp:TextBox>    <asp:RequiredFieldValidatorID="RequiredFieldValidator3"runat="server"ErrorMessage="Address is Required" ControlToValidate="TextBox3" ForeColor="Red">  
  36.                                     </asp:RequiredFieldValidator>  
  37.                     </td>  
  38.                 </tr>  
  39.                 <tr>  
  40.                     <td>                                                                                     
  41.                         <asp:ButtonID="Savebtn"runat="server"Text="Save"Width="62px"OnClick="Savebtn_Click" Style="margin-left: 5px" />  
  42.                         <asp:ButtonID="Updatebtn"runat="server"Text="Update"Width="63px"OnClick="Updatebtn_Click" Style="margin-left: 18px" />  
  43.                         <asp:ButtonID="Delete"runat="server"Style="text-align: left; margin-left: 15px;" Text="Delete" Width="58px" OnClick="Delete_Click" />  
  44.                     </td>  
  45.                 </tr>  
  46.                 <tr>  
  47.                     <tdstyle="text-align: left">                                                                     <asp:Label ID="Label4" runat="server" Style="text-align: center" Text="Label" Font-Bold="True" Font-Size="Large" ForeColor="#009900"></asp:Label>  
  48.                         </td>  
  49.                 </tr>  
  50.                 <tr>  
  51.                     <td>                                <asp:GridViewID="GridView1"runat="server"AutoGenerateSelectButton="True"OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" Style="margin-left: 265px;  
  52.   
  53. margin-top: -16px" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">  
  54.                             <Columns>  
  55.                                 <asp:BoundFieldDataField="ID"HeaderText="ID"ReadOnly="True"SortExpression="ID" />  
  56.                                 <asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name" />  
  57.                                 <asp:BoundFieldDataField="Email"HeaderText="Email"SortExpression="Email" />  
  58.                                 <asp:BoundFieldDataField="Address"HeaderText="Address"SortExpression="Address" />  
  59.                             </Columns>  
  60.                             <FooterStyleBackColor="#99CCCC"ForeColor="#003399" />  
  61.                             <HeaderStyleBackColor="#003399"Font-Bold="True"ForeColor="#CCCCFF" />  
  62.                             <PagerStyleBackColor="#99CCCC"ForeColor="#003399"HorizontalAlign="Left" />  
  63.                             <RowStyleBackColor="White"ForeColor="#003399" />  
  64.                             <SelectedRowStyleBackColor="#009999"ForeColor="#CCFF99"Font-Bold="true" />  
  65.                             <SortedAscendingCellStyleBackColor="#EDF6F6" />  
  66.                             <SortedAscendingHeaderStyleBackColor="#0D4AC4" />  
  67.                             <SortedDescendingCellStyleBackColor="#D6DFDF" />  
  68.                             <SortedDescendingHeaderStyleBackColor="#002876" />  
  69.                             </asp:GridView>  
  70.                     </td>  
  71.                 </tr>  
  72.       </table>  
  73.    </form>  
  74.   </body>  
  75. </html>  

Step 7

Now, add a reference to BLL to your solution explorer’s empty website template. Write the following code on the “Index.aspx.cs” page.

Index.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using BLL;  
  8. publicpartialclassIndext: System.Web.UI.Page {  
  9.     protectedvoid Page_Load(object sender, EventArgs e) {  
  10.         Label4.Visible = false;  
  11.     }  
  12.     protectedvoid Savebtn_Click(object sender, EventArgs e) {  
  13.         cslBLL clsBLLobj = newcslBLL();  
  14.         clsBLLobj.Insertvalues(TextBox1.Text, TextBox2.Text, TextBox3.Text);  
  15.         GridView1.DataSource = clsBLLobj.SelectValues();  
  16.         GridView1.DataBind();  
  17.         Label4.Visible = true;  
  18.         Label4.Text = "Data Insered";  
  19.     }  
  20.     protectedvoid Updatebtn_Click(object sender, EventArgs e) {  
  21.         cslBLL clsBLLobj = newcslBLL();  
  22.         clsBLLobj.UpdateValues(TextBox1.Text, TextBox2.Text, TextBox3.Text, Convert.ToInt32(HiddenField1.Value));  
  23.         GridView1.DataSource = clsBLLobj.SelectValues();  
  24.         GridView1.DataBind();  
  25.         Label4.Visible = true;  
  26.         Label4.Text = "Record Updated";  
  27.     }  
  28.     protectedvoid Delete_Click(object sender, EventArgs e) {  
  29.         cslBLL clsBLLobj = newcslBLL();  
  30.         clsBLLobj.Deletevalues(Convert.ToInt32(HiddenField1.Value));  
  31.         GridView1.DataSource = clsBLLobj.SelectValues();  
  32.         GridView1.DataBind();  
  33.         Label4.Visible = true;  
  34.         Label4.Text = "Record Deleted";  
  35.     }  
  36.     protectedvoid GridView1_SelectedIndexChanged(object sender, EventArgs e) {  
  37.         GridViewRow row = GridView1.SelectedRow;  
  38.         HiddenField1.Value = row.Cells[1].Text;  
  39.         TextBox1.Text = row.Cells[2].Text;  
  40.         TextBox2.Text = row.Cells[3].Text;  
  41.         TextBox3.Text = row.Cells[4].Text;  
  42.     }  
  43.     protectedvoid GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {  
  44.         if (e.Row.RowType == DataControlRowType.Header) {  
  45.             e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");  
  46.         }  
  47.         if (e.Row.RowType == DataControlRowType.DataRow) {  
  48.             e.Row.Cells[0].Style.Add(HtmlTextWriterStyle.Display, "none");  
  49.             e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);  
  50.         }  
  51.     }  
  52. }