In this Article you will learn how to get the selected row in a GridView and display the values in TextBoxes using ASP.NET. A GridView allows us to select only a single row at a time. The sample makes use of the database. We will be pulling data from the UserDetail table.
Creating Table in SQL Server Database
Now create a table named UserDetail with the columns UserID and UserName. The table looks as below.
Now insert data into the table.
First of all drag the GridView control from the Data controls menu. It will add the GridView control's HTML source code as given above.
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
You will need to set the Bound column in order to see the text in the cells of the GridView control.
.aspx source code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication120.WebForm1" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
UserID:
<asp:TextBox ID="TextBoxUserID" runat="server"></asp:TextBox>
<br />
UserName:
<asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" AutoGenerateColumns="False">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
<Columns>
<asp:BoundField HeaderText="UserID" DataField="UserID" />
<asp:BoundField HeaderText="UserName" DataField="UserName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
In the above GridView Properties set 'AutoGenerateSelectButton=True'. See the following image of a GridView after setting 'AutoGenerateSelectButton=True'. Select the GridView and press F4 for the property window.
See the Design view of your GridView. You will find a Hyperlink with a text as 'Select'.
Now add the following namespace.
using System.Data.SqlClient;
using System.Data;
Now write the connection string to connect to the database.
string strConnection = "Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;";
Now double-click on the page and write the following code for binding the data with the GridView.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication120
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
show();
}
private void show()
{
{
SqlConnection con = new SqlConnection("Data Source=.; uid=sa; pwd=wintellect;database=Rohatash;");
string strSQL = "Select * from UserDetail";
SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
dt.Fill(ds, "UserDetail");
con.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
Now run the application.
Selecting Row
Selecting the 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 the Gridview, double-Click on the SelectedIndexChanged Event and write the following code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBoxUserID.Text = GridView1.SelectedRow.Cells[1].Text;
TextBoxUserName.Text = GridView1.SelectedRow.Cells[2].Text;
}
Now run the application and select a row; that will show the selected row data in the TextBoxes.
Some Helpful Resources