C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
combo box Posted by j9070749 on 25 Oct 2012 at 1:40 PM
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:

            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();
        }


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

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


            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();



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
Report
Re: combo box Posted by j9070749 on 1 Nov 2012 at 6:28 AM
Any suggestions are appreciated.

Thanks
Report
Re: combo box Posted by j9070749 on 8 Nov 2012 at 3:58 AM
still having problems if anyone knows the solution.

Thanks
Report
Re: combo box Posted by Barnsite on 8 Nov 2012 at 5:38 AM
Hi, you need to clear the combobox before you loop and reload.

e.g. ComboBox.Items.Clear()
Report
Re: combo box Posted by Barnsite on 9 Nov 2012 at 12:46 AM
Rather than looping through your datatable you can just bind it to your dropdownlist, which will be faster, and easier


e.g.
//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;

Report
Re: combo box Posted by Barnsite on 9 Nov 2012 at 1:10 AM
Quick correction, that code was for a dropdownlist is ASP.net / C# for Combobox on a form you need slightly different:

//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;

Report
Re: combo box Posted by j9070749 on 9 Nov 2012 at 2:50 AM
Thank you for the help, I will try this over the weekend and let you know if it works.

Thanks

Andy



 

Recent Jobs