This article I am going to explain how to use gridview with the select button. Gridview provide built-in support for selection. Add a commanField column with the ShowSelectButton property set to true.
// ASPX
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
<style type=”text/css”>
.style1
{
width: 53px;
}
.style2
{
height: 65px;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”Label2″ runat=”server”Text=”Product Name”></asp:Label>
<asp:Label ID=”Label3″ runat=”server”Text=”Label”ForeColor=”Red”></asp:Label>
<br />
Quantity
<asp:Label ID=”Label4″ runat=”server”Text=”Label”ForeColor=”Red”></asp:Label>
<br />
Price
<asp:Label ID=”Label6″ runat=”server”Text=”Label”ForeColor=”Red”></asp:Label>
<br />
<table style=”width:100%;”>
<tr>
<tdclass=”style1″rowspan=”3″>
<asp:GridView ID=”GridView1″runat=”server”BackColor=”White”BorderColor=”#999999″
BorderStyle=”Solid” BorderWidth=”1px”CellPadding=”3″
DataKeyNames=”Productid,ProductName,UnitPrice”GridLines=”Vertical”
onrowcommand=”GridView1_RowCommand”
onselectedindexchanged=”GridView1_SelectedIndexChanged”ForeColor=”Black”>
<AlternatingRowStyleBackColor=”#CCCCCC”/>
<Columns>
<asp:CommandField ButtonType=”Button”ShowSelectButton=”True”/>
</Columns>
<FooterStyle BackColor=”#CCCCCC”/>
<HeaderStyle BackColor=”Black”Font-Bold=”True”ForeColor=”White”/>
<PagerStyle BackColor=”#999999″ForeColor=”Black”HorizontalAlign=”Center”/>
<SelectedRowStyleBackColor=”#000099″Font-Bold=”True”ForeColor=”White”/>
<SortedAscendingCellStyleBackColor=”#F1F1F1″/>
<SortedAscendingHeaderStyleBackColor=”#808080″/>
<SortedDescendingCellStyleBackColor=”#CAC9C9″/>
<SortedDescendingHeaderStyleBackColor=”#383838″/>
</asp:GridView>
</td>
<td>
</td>
</tr>
<tr>
<tdclass=”style2″>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
//C# Source Code
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;
public partial class _Default : System.Web.UI.Page
{
public int ProdID;
protected voidPage_Load(object sender, EventArgs e)
{
string connectionString = “Data Source=(local);Initial Catalog=Northwind;Integrated Security=True”;
SqlConnection cnn = newSqlConnection(connectionString);
string sql = “select ProductID,ProductName,QuantityPerUnit,UnitPrice from dbo.Products”;
SqlCommand cmd = newSqlCommand(sql,cnn);
SqlDataAdapter da = newSqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, “Product”);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected voidGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
ProdID = Convert.ToInt32(e.CommandArgument);
int ID = Convert.ToInt32(GridView1.DataKeys[ProdID].Values[0]);
Label3.Text = ID.ToString();
string PName= GridView1.DataKeys[ProdID].Values[1].ToString();
Label4.Text = PName.ToString();
string Price = GridView1.DataKeys[ProdID].Values[2].ToString();
Label6.Text = Price.ToString();
}
}