Please Refer this code
Ref:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView with RadioButtons</title>
<script type="text/javascript">
function RadioCheck(rb) {
var gv = document.getElementById("<%=GridView1.ClientID%>");
var rbs = gv.getElementsByTagName("input");
var row = rb.parentNode.parentNode;
for (var i = 0; i < rbs.length; i++) {
if (rbs[i].type == "radio") {
if (rbs[i].checked && rbs[i] != rb) {
rbs[i].checked = false;
break;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="green" AutoGenerateColumns="false"
Font-Names="Arial" OnPageIndexChanging="OnPaging" Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B"
AllowPaging="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" onclick="RadioCheck(this);" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("CustomerID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
<asp:BoundField ItemStyle-Width="150px" DataField="PostalCode" HeaderText="PostalCode" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#:
protected void Page_Load(object sender, EventArgs e)
{
// GetSelectedRecord();
ArrayList RadioButtonArray;
if (ViewState["RadioButtonArray"] != null)
{
RadioButtonArray = (ArrayList)ViewState["RadioButtonArray"];
}
else
{
RadioButtonArray = new ArrayList();
}
int CheckBoxIndex;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
RadioButton chk = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("RadioButton1");
CheckBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1);
if (chk.Checked)
{
if (RadioButtonArray.IndexOf(CheckBoxIndex) == -1)
{
RadioButtonArray.Add(CheckBoxIndex);
}
}
else
{
if (RadioButtonArray.IndexOf(CheckBoxIndex) != -1)
{
RadioButtonArray.Remove(CheckBoxIndex);
}
}
}
}
ViewState["RadioButtonArray"] = RadioButtonArray;
BindGrid();
}
private void BindGrid()
{
string strQuery = "select CustomerID,City,PostalCode" +
" from customers";
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager
.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
if (ViewState["RadioButtonArray"] != null)
{
ArrayList RadioButtonArray = (ArrayList)ViewState["RadioButtonArray"];
string checkAllIndex = "chkAll-" + GridView1.PageIndex;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
if (RadioButtonArray.IndexOf(checkAllIndex) != -1)
{
RadioButton chk = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("RadioButton1");
chk.Checked = true;
GridView1.Rows[i].Attributes.Add("style", "background-color:aqua");
}
else
{
int CheckBoxIndex = GridView1.PageSize * (GridView1.PageIndex) + (i + 1);
if (RadioButtonArray.IndexOf(CheckBoxIndex) != -1)
{
RadioButton chk = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("RadioButton1");
chk.Checked = true;
GridView1.Rows[i].Attributes.Add("style", "background-color:aqua");
}
}
}
}
}
}
}
NameSpace:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
Sql: