1

Please how can i display a selected radio button according to the value in the database ? example: i want to display the details of an employee with gender value (if the employee have "male" gender in the database i want that the radio button "Male" be selected automatically )

0

4 Answers 4

1

If you're using WPF, you can set a boolean property representing male gender. Then bind it to the radio button checked attribute.

In that way if male gender property returns true , the radio button will be checked.

Here is a good example:

Binding radio button

You can also use dependency property for this concept but I think the first method will work for you for sure.

Sign up to request clarification or add additional context in comments.

1 Comment

@leila.net you can use the following link to use same notion on win form RadioButton binding in Windows Forms with INotifyPropertyChanged?
1

it works succesfully for me, i just use a datagridview to compare the value from the database and the value of the radio button.

 GridView grid = new GridView();
    grid.ShowDialog(); 
if (grid.dataGridView1.CurrentRow.Cells[3].Value.ToString()=="Male")
{male_radiobtn.Checked=true;
female_radiobtn.Checked=false;}
else {female_radiobtn.Checked=true;
male_radiobtn.Checked=false;}

Comments

0

Using a BindingSource:

BindingSource myBindingSource = new BindingSource();
bindingSource.DataSource = (your datasource here, could be a DataSet or an object)

var maleBinding = new Binding("Checked", myBindingSource , "Gender");

maleBinding.Format += (s, args) => args.Value = ((string)args.Value) == "Male";
maleBinding.Parse += (s, args) => args.Value = (bool)args.Value ? "Male" : "Female";

maleRadioButton.DataBindings.Add(maleBinding);

var femaleBinding = new Binding("Checked", myBindingSource , "Gender");

femaleBinding.Format += (s, args) => args.Value = ((string)args.Value) == "Female";
femaleBinding.Parse += (s, args) => args.Value = (bool)args.Value ? "Female" : "Male";

femaleRadioButton.DataBindings.Add(femaleBinding);

Taken from here

1 Comment

In the Form Load event.
0

i have very simple method to display the database data in to the form right here.. i have Student Details database and in this database i have created info table. and i display the info table data.

 {
            // this code is to display data from the database table to application 
            int stdid = Convert.ToInt32(txtId.Text); // convert in to integer to called in sqlcmd
            SqlConnection con = new SqlConnection("Data Source=MILK-THINK\\SQLEXPRESS;Initial Catalog=Student Details;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from info where Student_id=" + stdid,con); // pass the query with connection 
            SqlDataReader dr; // this will read the data from database
            dr = cmd.ExecuteReader();
            if(dr.Read()==true)
            {
                txtName.Text = dr[1].ToString(); // dr[1] is a colum name which goes in textbox
                cmbCountry.SelectedItem = dr[2].ToString(); // combobox selecteditem will be display
               // for radio button we have to follow this method which in nested if statment
                if(dr[3].ToString()=="Male") 
                {
                    rdbMale.Checked=true;


                }
                else if (dr[3].ToString() == "Female")
                {
                    rdbFemale.Checked = true;
                }
                else
                {
                    MessageBox.Show("There are no records");
                    dr.Close();
                }
            }
            con.Close();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.