1

I am a .Net developer and I am searching for a question that how can I filter combobox values bound from a SQL Server 2012 database.

I have four comboboxes as seen in the Registration Form. I want to filter the values of all these comboboxes as if I select the value in first combobox that value should not be displayed in the other comboboxes.

I am using Visual Studio 2013.

Here is the code for first combobox of Course Name Field:

public void BindData()
{
    objcon.Open();
    string cmd = "select Course_Name from CourseDetails";

    objcom = new SqlCommand(cmd, objcon);
    objDA = new SqlDataAdapter(cmd, objcon);

    objDS = new DataSet();
    objDA.Fill(objDS);

    objcom.ExecuteNonQuery();
    objcon.Close();

    CmBx_Course_Name1.DisplayMember = "Course_Name";
    CmBx_Course_Name1.ValueMember = "Course_Name";
    CmBx_Course_Name1.DataSource = objDS.Tables[0];
    CmBx_Course_Name1.Enabled = true;
}

Here is the code for first combobox of Batch Name Field:

public void BindBatchName()
{
    objcon.Open();
    string cmd = "select Batch_Name from batch where Batch_Status IS NULL";

    objcom = new SqlCommand(cmd, objcon);
    objDA = new SqlDataAdapter(cmd, objcon);
    objDS = new DataSet();

    objDA.Fill(objDS);

    objcom.ExecuteNonQuery();

    objcon.Close();

    CmBx_batch_name1.DisplayMember = "Batch_Name";
    CmBx_batch_name1.ValueMember = "Batch_Name";
    CmBx_batch_name1.DataSource = objDS.Tables[0];
    CmBx_batch_name1.Enabled = true;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    Better to do it on client side and not with the requests. Load all comboboxes with all data and filter after user changes selected value in any of comboboxes. Try it first and share the results with us if you'll face another problem – ASpirin Sep 17 '17 at 07:20
  • You mean if else conditions??? – Assad Shehbaz Sep 17 '17 at 07:41
  • actually i have 8 batches and 4 combo-boxes under batch name and have 4 courses and 4 combo-boxes under course name field if i use if else conditions its really a hard task. – Assad Shehbaz Sep 17 '17 at 07:44
  • 1
    Bind each CBox via a BindingSource. Now they are both independent and can be sorted and filtered. See [here](https://stackoverflow.com/questions/23886653/comboboxes-are-linked-for-some-reason/23894146#23894146) and [here](https://stackoverflow.com/questions/33450813/bind-object-to-listbox/33451133#33451133) – TaW Sep 17 '17 at 09:53

0 Answers0