Refer the below sample code and implement it as per your code logic.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=gvDetails] .radioButtonList').on("click", function () {
var curretrow = $(this);
var isInactiveChecked = $(this).find('input[value="Inactive"]')[0].checked;
if (!isInactiveChecked) {
var row = $(this).closest('tr').closest('tr');
$('[id*=gvDetails] tr .radioButtonList').not(curretrow).each(function () {
var tr = $(this).closest('tr');
$(tr).find('input').each(function () {
if ($(this).val() == 'Active') {
$(this).removeAttr('checked');
}
else {
$(this).attr('checked', 'checked');
}
});
});
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="RowDataBound"
HeaderStyle-CssClass="HeaderCss">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:HiddenField ID="hfStatus" runat="server" Value='<%# Eval("Status")%>' />
<asp:RadioButtonList ID="rblStatus" runat="server" RepeatDirection="Horizontal" CssClass="radioButtonList">
<asp:ListItem Text="Active" Value="Active"></asp:ListItem>
<asp:ListItem Text="Inactive" Value="Inactive"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Status",typeof(bool)) });
dt.Rows.Add(1, "John Hammond", "true");
dt.Rows.Add(2, "Mudassar Khan", "false");
dt.Rows.Add(3, "Suzanne Mathews", "false");
dt.Rows.Add(4, "Robert Schidner", "false");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButtonList rblStatus = (RadioButtonList)e.Row.FindControl("rblStatus");
HiddenField hfStatus = (HiddenField)e.Row.FindControl("hfStatus");
if (hfStatus.Value.ToLower() == "true")
{
rblStatus.Items.FindByValue("Active").Selected = true;
}
else
{
rblStatus.Items.FindByValue("Inactive").Selected = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(System.Int32)), New DataColumn("Name", GetType(System.String)), New DataColumn("Status", GetType(System.Boolean))})
dt.Rows.Add(1, "John Hammond", "true")
dt.Rows.Add(2, "Mudassar Khan", "false")
dt.Rows.Add(3, "Suzanne Mathews", "false")
dt.Rows.Add(4, "Robert Schidner", "false")
gvDetails.DataSource = dt
gvDetails.DataBind()
End If
End Sub
Protected Sub RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim rblStatus As RadioButtonList = CType(e.Row.FindControl("rblStatus"), RadioButtonList)
Dim hfStatus As HiddenField = CType(e.Row.FindControl("hfStatus"), HiddenField)
If (hfStatus.Value.ToLower = "true") Then
rblStatus.Items.FindByValue("Active").Selected = True
Else
rblStatus.Items.FindByValue("Inactive").Selected = True
End If
End If
End Sub
Screenshot
