Using Radio buttons across multiple columns in GridView

Posted on 2010-01-04
Last Modified: 2013-12-17
Hi there

I have a table, that contains a number of rows, in each row there are three columns that are mutually exclusive.

What I need is to be able to have radio buttons that are linked across the three columns.
tocid       mandactionid       action       yes       no       notapp       comment
1       1       Action 1       1       0       0       Comment 1
1       2       Action 2       0       1       0       Comment 2
1       3       Action 3       1       0       0       Comment 3

I'd like for where there is a 1 for it to display a checked radio button, and where there is a 0 for it to be unchecked. This can then be edited by clicking the radio in another column where necessary.

Apologies if this is a little vague, I'm just beginning! Happy to post more detail if required.
Question by:k1ss0ff
    LVL 14

    Expert Comment

    try this

    in grid view add three radio buttons for the three columns and set there group names as

    radiobutton1 groupname='<%# "group" + Eval("mandactionid")%>'

    where i suppose mandactionid  is different for each row.

    in this way all the three radio buttons will have same group name and you can select only one of them...

    LVL 14

    Assisted Solution

    sorry missed some code

     yes                                                                                                                               no                                                                                                                          notapp
    radiobutton1 groupname='<%# "group" + Eval("mandactionid")%>'          radiobutton2 groupname='<%# "group" + Eval("mandactionid")%>'        radiobutton2 groupname='<%# "group" + Eval("mandactionid")%>'


    Author Comment

    I'm having some trouble getting it to accept a radio button at all.  I've attached the code so far...
    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true" %>
    <%@ Register assembly="System.Web.DynamicData, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.DynamicData" tagprefix="cc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="TOCIDs" DataTextField="tocname" DataValueField="tocid">
        <asp:ObjectDataSource ID="TOCIDs" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetTOCIDs" 
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                <asp:BoundField DataField="tocid" HeaderText="tocid" SortExpression="tocid" />
                <asp:BoundField DataField="mandactionid" HeaderText="mandactionid" 
                    SortExpression="mandactionid" />
                <asp:BoundField DataField="action" HeaderText="action" 
                    SortExpression="action" />
                <asp:TemplateField HeaderText="yes" SortExpression="yes">
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("yes") %>'></asp:Label>
                        <asp:RadioButton ID="RadioButton1" runat="server" 
                            Enabled='<%# Eval("yes") %>' />
                <asp:BoundField DataField="no" HeaderText="no" SortExpression="no" />
                <asp:BoundField DataField="notapp" HeaderText="notapp" 
                    SortExpression="notapp" />
                <asp:BoundField DataField="comment" HeaderText="comment" 
                    SortExpression="comment" />
        <EditItemTemplate >
                        <br />
        <asp:ObjectDataSource ID="MandatoryAnswersData" runat="server" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
                <asp:ControlParameter ControlID="DropDownList1" Name="tocid" 
                    PropertyName="SelectedValue" Type="Int32" />

    Open in new window

    LVL 16

    Accepted Solution

    Try it this way
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <asp:BoundField DataField="tocid" HeaderText="tocid" />
                    <asp:BoundField DataField="mandactionid" HeaderText="mandactionid" />
                    <asp:BoundField DataField="action" HeaderText="action" />
                    <asp:TemplateField HeaderText="yes">
                            <asp:RadioButton ID="rdYes" runat="server" GroupName="RGroup" Checked='<%#Eval("yes")%>' />
                    <asp:TemplateField HeaderText="no">
                            <asp:RadioButton ID="rdNo" runat="server" GroupName="RGroup" Checked='<%#Eval("no")%>' />
                    <asp:TemplateField HeaderText="notapp">
                            <asp:RadioButton ID="rdNotApp" runat="server" GroupName="RGroup" Checked='<%#Eval("notapp")%>' />
                    <asp:BoundField DataField="comment" HeaderText="comment" />

    Open in new window

    LVL 14

    Expert Comment

    you should have mentioned error also :)

    Author Comment

    Specified cast is not valid.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Getting there.  This is the error:

    Exception Details: System.InvalidCastException: Specified cast is not valid.

    Source Error:

    Line 33:                 <asp:TemplateField HeaderText="yes">
    Line 34:                     <ItemTemplate>
    Line 35:                         <asp:RadioButton ID="rdYes" runat="server" GroupName="RGroup" Checked='<%#Eval("yes")%>' />
    Line 36:                     </ItemTemplate>
    Line 37:                 </asp:TemplateField>

    The yes / no / notapp fields are currently tinyint(3) - do they need to be changed to something that's yes or no/true or false ?
    LVL 16

    Assisted Solution

    They should be of type boolean or you need to convert it like this
    <asp:RadioButton ID="rdYes" runat="server" GroupName="RGroup" Checked='<%#Convert.ToBoolean(Eval("yes"))%>' />

    Open in new window


    Author Comment

    Yay, fixed, many thanks :)

    Author Closing Comment

    Very helpful, many thanks for being patient!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now