none
Radio button does not get selected RRS feed

  • Question

  • I have some code, in which I want to select one of the two radio buttons on my form, based on the data in the database. The code is as below:

    string gender;
    using (OleDbConnection cn = new OleDbConnection(connectionString))
                {
                    cn.Open();
                    string scmd = "select [Customer_Name], [Address], [Contact_No], [Gender], [Issued_By] from Regular_Customers where Customer_Name = '" + cBoxCustomers.SelectedItem.ToString() + "'";
                    OleDbCommand cmd = new OleDbCommand(scmd, cn);
                    OleDbDataReader sdr = cmd.ExecuteReader();
                    while (sdr.Read())
                    {
                        txtName.Text = sdr[0].ToString(); ;
                        txtAddress.Text = sdr[1].ToString();
                        txtContactNo.Text = sdr[2].ToString();
    			 gender = sdr[3].ToString();
     MessageBox.Show("Gender is: "+gender);
                        
                        if (gender == "M")
                        {
                            radioBtnMale.Select();
                        }
    
                        if (gender == "F")
                        {
                            radioBtnFemale.Select();
                        }
    
                        txtIssuedBy.Text = sdr[4].ToString();
                    }
                }


    I have used this MessageBox to see what is getting stored in the variable gender. The value M/F are coming properly in the gender variable. But the radio buttons does not get selected. Can anyone suggest the solution?

     


    • Edited by tapan.desai Monday, September 26, 2011 3:51 PM
    Monday, September 26, 2011 3:50 PM

Answers

  • Try this

     

    if (gender == "M")
                        {
                            radioBtnMale.Checked = True;
                        }
    
                        if (gender == "F")
                        {
                            radioBtnFemale.Checked = True;
    
                        }

    Happy Coding, RDRaja
    • Edited by Dharmalinga Raja Tuesday, September 27, 2011 1:04 AM
    • Proposed as answer by Louis.fr Wednesday, September 28, 2011 8:54 AM
    • Marked as answer by tapan.desai Thursday, September 29, 2011 7:06 AM
    Monday, September 26, 2011 4:13 PM

All replies

  • Try this

     

    if (gender == "M")
                        {
                            radioBtnMale.Checked = True;
                        }
    
                        if (gender == "F")
                        {
                            radioBtnFemale.Checked = True;
    
                        }

    Happy Coding, RDRaja
    • Edited by Dharmalinga Raja Tuesday, September 27, 2011 1:04 AM
    • Proposed as answer by Louis.fr Wednesday, September 28, 2011 8:54 AM
    • Marked as answer by tapan.desai Thursday, September 29, 2011 7:06 AM
    Monday, September 26, 2011 4:13 PM
  • RadioButton.Checked is the property to select it.
    Mitja
    Monday, September 26, 2011 4:14 PM
  •  if (gender == "M")
                        {
                            radioBtnMale.Checked=True;
                        }
    
                        if (gender == "F")
                        {
                            radioBtnFemale.Checked=True;
                        }

    if  you have any doubt then reply me.
    Monday, September 26, 2011 5:43 PM
  • This is a sign that you did not create the radio buttons properly.

    The radio buttons need to be associates to each other.

    You should use a panel, groupbox or create a radio button list in order to achieve this.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Tuesday, September 27, 2011 6:52 AM
  • If you are sure the gender is only F or M than only select one of those. 

    A radio button in a container which is set will direct deselect all other radio buttons in the containers.

    If you want that you can have both F and M gender the same time use a checkbox with the radiobutton property for that set to true.

     


    Success
    Cor
    Tuesday, September 27, 2011 8:24 AM
  • Select() will put the focus on a control. Use the Checked property.
    Wednesday, September 28, 2011 8:56 AM
  • Hi tapan.desai,
    Welcome to the MSDN forum!
     

    How is it going with the question?

    Please refer to the following link to get more information about RadioButton.Checked Property:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checked.aspx

    Have a nice day!


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, September 28, 2011 12:54 PM
    Moderator
  • Hi tapan.desai,
    Welcome to the MSDN forum!
     

    How is it going with the question?

    Got the correct answer nice and fast.
    Thursday, September 29, 2011 7:08 AM
  • Congratulations!
    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 29, 2011 7:12 AM
    Moderator