Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I fetch a table data into dataset, which is holding gender values also, I need to check the condition if radio button dataset column=male then select male radio button,
this is my code...Need bit of support
C#
if (ds.Tables[0].Rows[i]["Gender"].ToString() == "M")
                    {
                        rbtmale.Checked = true;
                    }
                    else
                    {
                        rbtfemale.Checked = true;
                    }
Posted
Comments
[no name] 6-Apr-15 7:29am    
not clear , what problem you face in radio button selection ,
Please Explain briefly
Meer Wajeed Ali 6-Apr-15 7:48am    
In dataset I am having gender, at the time assigning into gender, if column value is M, select radio button Male or else female
CHill60 6-Apr-15 8:30am    
Your code would appear to do that - what is happening when you run it?
Meer Wajeed Ali 6-Apr-15 8:35am    
When I run, radio buttons those have to check according to gender, and this is windows forms app
CHill60 6-Apr-15 9:20am    
I know what you want it to do. What actually happens? Your code does not appear to be wrong in any way

There is nothing wrong with that code.

Radiobuttons are designed so that automatically only one per container can be Checked at any one time. So it is perfectly valid to just set the Checked value as you have done.

If these are the only RadioButtons on the form then there is no need to put them into a groupbox or any other container. I do put radiobuttons into a container for preference as it makes it easier if anything is added later, they need to be re-sited on the form, or if they need to be replaced if a Winforms RadioButtonList control comes along.

If they are not behaving as you expected then it can be one of three things


  1. (If both RBs are displayed as checked) You have placed the RadioButtons into two separate container controls on the form. Make sure they are in the same one.
  2. (If it is the case that the Female RB is always checked) Your dataset doesn't actually contain "M" in the gender Column at all.
  3. (If a different RB is checked to the one you were expecting) If this code is in a loop, you will only display the gender of the last record
 
Share this answer
 
Hello,
first do this ,take radiobutton list instead of radio buttons for gender because user could select either MALE or FEMALE , can't be select BOTH :)
so take radiobutton list and try with this ,

XML
<asp:RadioButtonList ID="radGender" runat="server" RepeatDirection="Horizontal" Width="75%">
                      <asp:ListItem Value="M">Male</asp:ListItem>
                      <asp:ListItem Value="F">Female</asp:ListItem><br />
                  </asp:RadioButtonList>


C#
if (ds.Tables[0].Rows[i]["Gender"].ToString() == "M")
                    {
                        radGender.SelectedValue = "M";
                    }
                    else
                    {
                        radGender.SelectedValue = "F";
                    }

If its work for you , acknowledge here.
Thank you.
 
Share this answer
 
Comments
CHill60 6-Apr-15 8:31am    
This looks like an ASP solution not a Windows Forms solution?
Meer Wajeed Ali 6-Apr-15 8:37am    
This one is windows app, kindly suggest me asap...
In windows form ,
take GROUP BOX and drag 2 radio button in it , give id and values ,
so u will select any one at a time.
then in code behind write code.
C#
if (ds.Tables[0].Rows[i]["Gender"].ToString() == "M")
                    {
                        radMale.SelectedValue = "M";
                    }
                    else
                    {
                        radFemale.SelectedValue = "F";
                    }


:)
Thank you.
 
Share this answer
 
v3
Comments
CHill60 6-Apr-15 9:28am    
This is just completely and utterly wrong. There is no such property on a RadioButton as "SelectedValue"
CHill60 6-Apr-15 9:38am    
I don't understand your comment
Meer Wajeed Ali 6-Apr-15 10:09am    
Thanx I solved this one...Happy coding
i tried this , its working.
you should try this.
:)
CHill60 18-Aug-15 7:03am    
No it doesn't work. Following the instructions in your "solution" you get the error : "'System.Windows.Forms.RadioButton' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'System.Windows.Forms.RadioButton' could be found (are you missing a using directive or an assembly reference?)".
If this works for you then you are not using C# Windows Forms RadioButtons
C#
if (ds.Tables["EmpBasicInfo"].Rows[i]["Gender"].ToString() == "M")
                   {
                       rbtmale.Checked = true;
                   }
                   else
                   {
                       rbtfemale.Checked = true;
                   }
 
Share this answer
 
Comments
CHill60 6-Apr-15 10:07am    
Is this the solution? Table "EmpBasicInfo" was not the first Table in the collection?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900