Learn how to a build a cloud-first strategyRegister Now


Using Radio buttons across multiple columns in GridView

Posted on 2010-01-04
Medium Priority
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
  • 4
  • 3
  • 2
LVL 14

Expert Comment

ID: 26170941
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

amar31282 earned 400 total points
ID: 26170975
sorry missed some code

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


Author Comment

ID: 26171224
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

LVL 16

Accepted Solution

sunithnair earned 1600 total points
ID: 26171320
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

ID: 26171328
you should have mentioned error also :)

Author Comment

ID: 26171414
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

sunithnair earned 1600 total points
ID: 26171449
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

ID: 26171537
Yay, fixed, many thanks :)

Author Closing Comment

ID: 31672363
Very helpful, many thanks for being patient!

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 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