Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

combo box

j9070749j9070749 Posts: 7Member
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

  • j9070749j9070749 Posts: 7Member
    Any suggestions are appreciated.

    Thanks
  • j9070749j9070749 Posts: 7Member
    still having problems if anyone knows the solution.

    Thanks
  • BarnsiteBarnsite Posts: 4Member
    Hi, you need to clear the combobox before you loop and reload.

    e.g. ComboBox.Items.Clear()
  • BarnsiteBarnsite Posts: 4Member
    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]
  • BarnsiteBarnsite Posts: 4Member
    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]
  • j9070749j9070749 Posts: 7Member
    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.