Click here to Skip to main content
13,191,642 members (68,392 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 23 Aug 2004

A simple RadioButtonList in a Web DataGrid Column

, 23 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A very simple and practical way, how a single RadioButton acts as a RadioButtonList in a web DataGrid server control column.


This article exposes, in a very simple and practical way, how a single RadioButton acts as a RadioButtonList in a web DataGrid server control column. It’s nothing about two thousand lines of code, nor custom control, neither signed assembly. I always wonder why do complicated when we can do simple.


First, in the ItemCreated DataGrid event, I’ll wire the RadioButton CheckedChanged event:

private void DataGrid1_ItemCreated(object sender, 
   System.Web.UI.WebControls.DataGridItemEventArgs e)
   //Find the RadioButton control
   RadioButton oRb = ((RadioButton)e.Item.FindControl("rb"));             
   if(oRb != null)
      //Wire the RadioButton's event
      oRb.CheckedChanged += new

Second, in the RadioButton CheckedChanged event, I’ll loop through DataGrid items to find the previous and current radio buttons and, if found, the previously checked button will be unchecked:

private void rb_CheckedChanged(object sender, System.EventArgs e)
   //Find the current selected RadioButton
   RadioButton oRb1 = (RadioButton)sender;

   foreach(DataGridItem oItem in DataGrid1.Items)
      //Find the previous selected RadioButton
      RadioButton oRb2 = (RadioButton)oItem.FindControl("rb");

      //Display info for the current selected button
      if( oRb2.Equals(oRb1) )
         Message.Text = 
            String.Format("Selected Id is: {0}, 
                         and the Description is:{1}",
       //Uncheck previously selected button
          oRb2.Checked = false;

The info is displayed in a message label after I’ve recuperated the lblId and lblDesc text from the HTML web form, like in the code below:

<asp:label id="Message" Runat="server"></asp:label>
   <asp:TemplateColumn HeaderText="Id">
         <asp:Label id="lblId" runat="server" Text='<%# 
            DataBinder.Eval(Container.DataItem, "Id") %>' />

   <asp:TemplateColumn HeaderText="Description">
         <asp:Label id="lblDesc" runat="server" Text='<%# 
            DataBinder.Eval(Container.DataItem,"Description") %>' />

The DataGrid is bound to an ArrayList aList in the BindDataGrid():

private void BindDataGrid()
   ArrayList aList = new ArrayList();
   for(int i=1; i<6; i++)
      aList.Add(new ItemInfo(i, "Desc " + i.ToString()));

   //Binding and displaying the info in the DataGrid
   DataGrid1.DataSource = aList;

For more details, please download the source code above. You have nothing else to do than to create a new C# project for an ASP.NET application and add the source code files. That’s all about it.



This article, along with any associated source code and files, is licensed under Microsoft Reciprocal License


About the Author

Web Developer
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

Generalthe same in detail datagrid Pin
asily31-Oct-05 12:22
memberasily31-Oct-05 12:22 
Generaldon't need to be this complicated... Pin
Anonymous19-Sep-05 16:04
sussAnonymous19-Sep-05 16:04 
GeneralDoesn't work as user control Pin
theredder10-Apr-05 17:58
membertheredder10-Apr-05 17:58 
GeneralRe: Doesn't work as user control Pin
Andrés Ortíz14-Apr-05 12:13
memberAndrés Ortíz14-Apr-05 12:13 
GeneralRe: Doesn't work as user control Pin
x_h1b_y3-Jul-06 12:57
memberx_h1b_y3-Jul-06 12:57 
GeneralWhy things are complicated Pin
seby17215729-Oct-04 0:40
memberseby17215729-Oct-04 0:40 
GeneralRe: Why things are complicated Pin
cojocam9-Nov-04 8:16
membercojocam9-Nov-04 8:16 
GeneralFor VB.NET Pin
hoahong26-Aug-04 17:21
memberhoahong26-Aug-04 17:21 
GeneralRe: For VB.NET Pin
cojocam27-Aug-04 3:14
membercojocam27-Aug-04 3:14 
QuestionHow to Convert this Source into Vb.NET Pin
hoahong26-Aug-04 16:44
memberhoahong26-Aug-04 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.2 | Last Updated 24 Aug 2004
Article Copyright 2004 by Socrate1
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid