2

I am working with WinForm in this i have 3 RadioButton one ComboBox and have three BindingSource

I want that when I check any of the RadioButtons, the values from the particular DataSources get bound to the ComboBox's ValueMember and DisplayMember.
The program has a SQL query where I want to send value based on the checked radio button.

private void rdbMed_CheckedChanged(object sender, EventArgs e)
{
    cmbStock.DataSource = null;
    cmbStock.DataSource = this.medicinesBindingSource;
    this.cmbStock.DisplayMember = ""; //i want to bind "Med_ID" from medicinesBindingSource
    this.cmbStock.ValueMember = "";  //"Med_Name"

}

private void rdbCat_CheckedChanged(object sender, EventArgs e)
{
    cmbStock.DataSource = null;
    cmbStock.DataSource = this.categoriesBindingSource;
    this.cmbStock.DisplayMember = ""; //"category_Name"
    this.cmbStock.ValueMember = ""; // category_ID"
}

private void rdbPharm_CheckedChanged(object sender, EventArgs e)
{
    cmbStock.DataSource = null;
    cmbStock.DataSource = this.pharmaceuticalBindingSource;
    this.cmbStock.DisplayMember = "";// "Pharma_Name"
    this.cmbStock.ValueMember = "";// "Pharma_ID"
}

Bellow are the parameter of the Query. It will help you to understand what I want to achieve.

if (rdbMed.Checked)
{
    con.cmd.Parameters.AddWithValue("@Med_ID", cmbStock.ValueMember);
    con.cmd.Parameters.AddWithValue("@category_ID", "");
    con.cmd.Parameters.AddWithValue("@Pharma_ID", "");
}
else if (rdbCat.Checked)
{
    con.cmd.Parameters.AddWithValue("@Med_ID", "");
    con.cmd.Parameters.AddWithValue("@category_ID", cmbStock.ValueMember);
    con.cmd.Parameters.AddWithValue("@Pharma_ID", "");
}
else if (rdbPharm.Checked)
{
    con.cmd.Parameters.AddWithValue("@Med_ID", "");
    con.cmd.Parameters.AddWithValue("@category_ID", "");
    con.cmd.Parameters.AddWithValue("@Pharma_ID", cmbStock.ValueMember);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Danish
  • 343
  • 1
  • 8
  • 21
  • 1
    You would put your Property names in the DisplayMember and ValueMember properties (what looks like is commented out next to each property). You use the cmbStock.SelectedValue to retrieve the value then. Check for null just in case. Don't leave your connections and commands open like that — put it all in using-blocks so that they're disposed when not in use. – LarsTech Oct 26 '18 at 22:57
  • Yes,Thank you it is working as i was expecting. i was thinking if i put it there ,it will be equal to assigning "Med_ID" or "Med_Name" to DisplayMember or ValueMember. – Danish Oct 26 '18 at 23:39
  • DisplayMember is what gets shown on the screen, ValueMember is the data you would care about. – LarsTech Oct 27 '18 at 00:56

1 Answers1

1

Easiest and the least pain way of doing this is resolving it inside SQL command and way to do it is cast your columns with AS VM and AS DM and make your DisplayMember = "DM" and ValueMember = "VM", so if you have sql query like SELECT USERID AS VM, NAME AS DM or SELECT PRODUCTID AS VM, NAME AS DM it will both work.

Problem later is if you do something with code and you may make mistakes trying to get USERID from databinding but it is instead VM. To avoid this you could "double select" values in query like SELECT USERID, NAME, ...., USERID AS VM, NAME AS DM this way you will have VM and DM for your controls but still hold original columns.

Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54