Adding RadioButtonList to specific rows in a column based on a condition

Last Reply on Mar 14, 2016 06:25 AM By Shashikant

Posted on Mar 14, 2016 12:04 AM

Hi! I have an excel data imported into a datatable and I appended an extra column in the datatable. I want to add a radiobutton list dynamically to specific cells in the appended column using a condition before I bind the whole table to DataGridView.

Eg: ID and Name from Excel sheet and Sex dynamically appended data column.

|  ID  |  Name  |  Sex  |

  A01       Rob                 

  B02       Mike

  A03       Alex

  B04       Steve

This how the data looks like in the Datatable through Data Visualiser.
I want to traverse through every datarow in the data table and assign a radiobuttonlist (Male, Female) in the Sex datacolumn cell where the ID cell of that row contains an 'A'. How do I achieve this dynamically?

Posted on Mar 14, 2016 06:25 AM


Please refer below code


    <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" OnRowDataBound="Data_RowDataBound">
            <asp:TemplateField HeaderText="Id">
                    <asp:Label ID="lblId" Text='<%#Eval("ID") %>' runat="server" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:TemplateField HeaderText="Gender">
                    <asp:RadioButtonList ID="rbtnSex" runat="server">


protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        DataTable dt = new DataTable();
        dt.Rows.Add("A01", "Rob");
        dt.Rows.Add("B02", "Mike");
        dt.Rows.Add("A03", "Alex");
        dt.Rows.Add("B04", "Steve");
        gvData.DataSource = dt;

protected void Data_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        if ((e.Row.FindControl("lblId") as Label).Text.Contains("A"))
            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem("Male", "1"));
            items.Add(new ListItem("Female", "2"));
            RadioButtonList rbtnList = (e.Row.FindControl("rbtnSex") as RadioButtonList);
            rbtnList.DataSource = items.ToArray();