Hi rajeesh,
Refer below sample.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="itemcategory" HeaderText="Category" />
<asp:BoundField DataField="itemname" HeaderText="Item Name" />
<asp:BoundField DataField="price" HeaderText="Price" />
<asp:TemplateField HeaderText="Size">
<ItemTemplate>
<br />
<asp:RadioButtonList ID="rblsize" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>6</asp:ListItem>
<asp:ListItem>7</asp:ListItem>
<asp:ListItem>8</asp:ListItem>
<asp:ListItem>9</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id", typeof(int)), new DataColumn("itemcategory", typeof(string)), new DataColumn("itemname", typeof(string)), new DataColumn("price", typeof(int)) });
dt.Rows.Add(1, "Stationary", "Pen", 10);
dt.Rows.Add(2, "Stationary", "Pencile", 10);
dt.Rows.Add(3, "Stationary", "Eraser", 10);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// Code for rowcommand
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
RadioButtonList rblSize = gvRow.FindControl("rblsize") as RadioButtonList;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
// Code for selectedindexchanging
int index = e.NewSelectedIndex;
RadioButtonList rblSize = GridView1.Rows[index].FindControl("rblsize") as RadioButtonList;
}
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(Integer)), New DataColumn("itemcategory", GetType(String)), New DataColumn("itemname", GetType(String)), New DataColumn("price", GetType(Integer))})
dt.Rows.Add(1, "Stationary", "Pen", 10)
dt.Rows.Add(2, "Stationary", "Pencile", 10)
dt.Rows.Add(3, "Stationary", "Eraser", 10)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim gvRow As GridViewRow = GridView1.Rows(index)
Dim rblSize As RadioButtonList = TryCast(gvRow.FindControl("rblsize"), RadioButtonList)
End Sub
Protected Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As GridViewSelectEventArgs)
Dim index As Integer = e.NewSelectedIndex
Dim rblSize As RadioButtonList = TryCast(GridView1.Rows(index).FindControl("rblsize"), RadioButtonList)
End Sub