Select row by clicking in a GridView
GridViewSelectRow.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewSelectRow.aspx.cs"
Inherits="GridViewSelectRow" EnableEventValidation="false" %>
<!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>Select row by clicking in a GridView</title>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<tr>
<td>
SELECTED ID
</td>
<td>
<asp:Label ID="sID" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
SELECTED UNIQUEID
</td>
<td>
<asp:Label ID="sUniqueID" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
SELECTED PRICE
</td>
<td>
<asp:Label ID="sPrice" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
SELECTED AMOUNT
</td>
<td>
<asp:Label ID="sAmount" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td>
SELECTED CREATEDATE
</td>
<td>
<asp:Label ID="sCreateDate" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,UniqueID"
DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
AllowPaging="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="UniqueID" HeaderText="UniqueID" ReadOnly="True" SortExpression="UniqueID" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />
<asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" />
</Columns>
</asp:GridView>
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:brucedbConnectionString %>"
SelectCommand="SELECT * FROM [GV]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
GridViewSelectRow.aspx.cs
using System;
using System.Collections;
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;
public partial class GridViewSelectRow : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridView)sender).SelectedRow;
this.sID.Text = row.Cells[0].Text;
this.sUniqueID.Text = row.Cells[1].Text;
this.sPrice.Text = row.Cells[2].Text;
this.sAmount.Text = row.Cells[3].Text;
this.sCreateDate.Text = row.Cells[4].Text;
}
}