Tuesday, January 12, 2021

How To Get The Values Of Selected Row From A Gridview Using ASP.NET

 In this Article you can learn how to get the values of selected row from a Gridview and display the values in textBoxes using C# code.

 

Selecting a Row in Gridview

 
Add one Gridview and bind a Data using SqlDataSource.and Add three Textboxes to display a data in Textboxes when the GridviewRow is Selected.
 
Ste the Gridview AutoGenerateColumns property to 'False'.
 
You will need to set the Bound column in order to see the text in the cells of the grid view control.
  1. <asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged Width="306px" Height="137px">  
  2.     <Columns >  
  3.     <asp:BoundField HeaderText ="Rowid"  DataField ="Rowid" />  
  4.     <asp:BoundField HeaderText ="Name" DataField ="Name" />  
  5.     <asp:BoundField HeaderText ="Marks" DataField ="marks" />     
  6.     </Columns>  
  7.    </asp:GridView>  
  8.  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>" SelectCommand="SELECT * FROM [emp]">  
  9. </asp:SqlDataSource>  
In Gridview Properties set 'AutoGenerateSelectButton=True'.See below image of Gridview after setting 'AutoGenerateSelectButton=True'.

How To Get The Values Of Selected Row From A Gridview Using ASP.NET

See the Desing view of your Gridview.you will find a Hyperlink with a text as 'Select'.
 

Selecting Row

 
Selecting row event is fired when you make a click on the select link. If you need any particular item in that row you can easily select it using the cells property:
 
In Gridview Events double Click on SelectedIndexChanged Event and write the below code,
  1. protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)  
  2. {  
  3.     txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;  
  4.     txtname.Text = Gridview1.SelectedRow.Cells[2].Text;  
  5.     txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text;  
  6. }  

How To Get The Values Of Selected Row From A Gridview Using ASP.NET

Complete .asp source code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml" >  
  5.     <head id="Head1" runat="server">  
  6.         <title>Untitled Page</title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <asp:GridView runat ="Server" ID="Gridview1" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AutoGenerateSelectButton="True" OnSelectedIndexChanged="Gridview1_SelectedIndexChanged" left: 266px; position: GridLines="None" Width="306px" Height="137px">  
  12.                     <Columns >  
  13.                         <asp:BoundField HeaderText ="Rowid" DataField ="Rowid" />  
  14.                         <asp:BoundField HeaderText ="Name" DataField ="Name" />  
  15.                         <asp:BoundField HeaderText ="Marks" DataField ="marks" />  
  16.                     </Columns>  
  17.                     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  18.                     <SelectedRowStyle BackColor="Teal" ForeColor="Maroon" Font-Bold="True" />  
  19.                     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  20.                     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  21.                     <AlternatingRowStyle BackColor="White" />  
  22.                     <RowStyle BackColor="#EFF3FB" />  
  23.                     <EditRowStyle BackColor="#2461BF" />  
  24.                 </asp:GridView>  
  25.                 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shakeerConnectionString %>"  
  26. SelectCommand="SELECT * FROM [emp]">  
  27.                 </asp:SqlDataSource>  
  28.                 <br />  
  29.                 <br />  
  30.                 <asp:TextBox ID="txtrowid" runat="server" Style="z-index: 101; left: 365px; position: absolute;top: 251px"></asp:TextBox>  
  31.                 <asp:TextBox ID="txtname" runat="server" Style="z-index: 102; left: 365px; position: absolute;top: 283px"></asp:TextBox>  
  32.                 <asp:TextBox ID="txtmarks" runat="server" Style="z-index: 103; left: 365px; position: absolute; top: 315px"></asp:TextBox>  
  33.                 <asp:Label ID="Label1" runat="server" Style="z-index: 107; left: 305px; position: absolute;top: 253px" Text="RowId"></asp:Label>  
  34.                 <asp:Label ID="Label2" runat="server" Style="z-index: 105; left: 303px; position: absolute;  
  35. top: 287px" Text="Name"></asp:Label>  
  36.                 <asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 306px; position: absolute;  
  37. top: 318px" Text="Marks"></asp:Label>  
  38.             </div>  
  39.         </form>  
  40.     </body>  
  41. </html>  

In .aspx.cs code,

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10.   
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.   
  16.     }  
  17.     protected void Gridview1_SelectedIndexChanged(object sender, EventArgs e)  
  18.     {  
  19.         txtrowid.Text = Gridview1.SelectedRow.Cells[1].Text;  
  20.         txtname.Text = Gridview1.SelectedRow.Cells[2].Text;  
  21.         txtmarks.Text = Gridview1.SelectedRow.Cells[3].Text;  
  22.     }  
  23. }  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...