combo box

Hi, I am creating a c# win form application.

I have 2 comboboxes.

One named suppliercombobox, the other named contactcombobox.

In my C# application I have a sql database linked in.

1 Supplier can have many contacts, one contact can only work for one supplier.

Below is the code which puts the values in the combobox suppliercombobox:

[code]
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblsupplier", con);

DataTable dt = new DataTable();
da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
SupplierComboBox.Items.Add(dt.Rows[i]["SupplierName"]);
}
con.Close();
}
[/code]

This code is called on the page load event, and works fine.

My code to put the values in the contact combo box is:

[code]

con.Open();

string supid = SupplierComboBox.SelectedIndex.ToString();
MessageBox.Show(supid);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblsuppliercontact WHERE supplierid= '"+ supid +"'", con);
DataTable dt = new DataTable();
dt.Rows.Clear();

dt.Clear();
da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++) // put a breakpoint here and look at the table it the datatable, it is the correct data but is not showing correclty in the listbox.
{
ContactcomboBox.Items.Add(dt.Rows[i]["ContactFirstName"]);
}
da = null;
dt.Rows.Clear();
con.Close();

[/code]

This code called when the text changes in the supplier combo box. Initially it will load the contacts for the correct supplier, but if I change the supplier again, the contact combo box will display the contacts from BOTH of the suppliers.

I have tried using dt.rows.clear(); and dt.clear(); in various places.

I would like to know how to get the contact combo box working as I descibed (to only show the contacts from the selected supplier).

I would also like to know how to display both the contacts firstname and surname in the combo drop down box.

I am very new to c# so unsure if I am approaching this correctly, please ask if you need to know anything else, any help is greatly appreiciated, I've been stuck on this a while now.

Many Thanks

Comments

  • Any suggestions are appreciated.

    Thanks
  • still having problems if anyone knows the solution.

    Thanks
  • Hi, you need to clear the combobox before you loop and reload.

    e.g. ComboBox.Items.Clear()
  • Rather than looping through your datatable you can just bind it to your dropdownlist, which will be faster, and easier :-)


    e.g.
    [code]
    //set datasource of combo to dt
    ContactcomboBox.DataSource = dt;

    //set the text and value fields
    ContactcomboBox.DataTextField = "ContactFirstName";
    ContactcomboBox.DataValueField = "ContactFirstName";

    //bind the data to the combo
    ContactcomboBox.DataBind();

    //add a blank first row to the combo (optional)
    ContactcomboBox.Items.Insert(0,new ListItem(string.Empty, string.Empty);

    //set the selected value to the first list value (optional)
    ContactcomboBox.SelectedIndex = 0;
    [/code]
  • Quick correction, that code was for a dropdownlist is ASP.net / C# for Combobox on a form you need slightly different:

    [code]
    //set datasource of combo to dt
    ContactcomboBox.DataSource = dt;

    //set the display text
    ContactcomboBox.DisplayMember = "ContactFirstName";

    //set the selected value to the first list value (optional)
    ContactcomboBox.SelectedIndex = 0;
    [/code]
  • Thank you for the help, I will try this over the weekend and let you know if it works.

    Thanks

    Andy
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories