Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Combo box default value

I am trying to make SQL statement based on item selected throw combo box. I want to have default item selected as ID, but now it returns NULL.
What am I doing wrong?

private void Win_Shown(object sender, EventArgs e)
{
   myBox.SelectedValue = "ID";
   myBox.SelectedText = "ID";
   myBox.SelectedItem = "ID";

   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 
}

Then in for SQL statement

MySQL.DisplayAndSearch("SELECT * FROM Data WHERE " + this.myBox.SelectedItem.ToString() + " LIKE '%" + txt_Search.Text + "%'", dataGridView1);

Thank for any help 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are selecting the item before inserting:

private void Win_Shown(object sender, EventArgs e)
{
   // first, add the items   
   myBox.Items.Add("ID");
   myBox.Items.Add("Name");
   myBox.Items.Add("Surname");
   myBox.Items.Add("Mobile"); 

   // now select it
   myBox.SelectedItem = "ID";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading