Link to home
Start Free TrialLog in
Avatar of jackjohnson44
jackjohnson44

asked on

c# combobox values bound from database, and adding initial values

I have several comboboxes which are being built from values in a database, usually a name and a guid for a value.

They will be used in a query filter, so values could be "All, None, Item1, Item2, Item3.  

Since All and None don't exist in the database, I need to add them to the combobox.  Since they don't have GUIDs, I have to either use a blank guid, or a blank value.  How do people do this?  Since I can't use a my datatable since I am adding values.
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Use InsertAt method of the combobox after it is bound to add values
Avatar of Bob Learned
Guid.Empty is the usual representation for None.  It represents this value:

"00000000-0000-0000-0000-000000000000"

Bob
See this

http://www.thescripts.com/forum/thread165843.html

basically you do

ComboBox1.Items.InsertAt(0, "All")
pComboName.DataSource = oDataHelper.moDataSet.Tables[0];
            pComboName.DataTextField = pDataTextField;
            pComboName.DataValueField = pDataValueField;
            pComboName.DataBind();
     
                ListItem listItem = new ListItem();
                listItem.Text = "All";
                listItem.Value = "0";
                pComboName.Items.Insert(0, listItem);
Avatar of jackjohnson44
jackjohnson44

ASKER

this is a windows applicaiton, isn't listbox only a web collection?  What namespace do I have to use?
ListItem is an ASP.NET class.  You need System.Windows.Forms.ListBox.

Bob
That does not work I am getthign this error
"Items collection cannot be modified when the DataSource property is set."



                cmbTestPlanCode.DataSource = m_ds.TestPlanCodes;
                cmbTestPlanCode.DisplayMember = m_ds.TestPlanCodes.TestPlanCodeColumn.ColumnName;
                cmbTestPlanCode.ValueMember = m_ds.TestPlanCodes.TestPlanCodeColumn.ColumnName;

                ListBox lb = new ListBox();
                lb.Text = "ALL";
                lb.ValueMember = "0";
                cmbTestPlanCode.Items.Insert(0, lb);
Add the items to the data source, and refresh the ComboBox.

Bob
I don't see how that will help.  First of all, I want to add 2 values, so I would have to add two rows, with a guid of the "00000000-0000-0000-0000-000000000000", which will not work since it is the primary key.  I suppose I could use another guid, then check for it later, but that seems like a hack.  Even if that did work though, the sorting would be out of wack.

Has anyone actually done this?  These answers seem to be guesses.   I appreciate the help, but this is a pretty big waste of time.  I was hoping someone who has done this will propose an answer.  None of the answers even came remotely close to working.
ASKER CERTIFIED SOLUTION
Avatar of sakshi_net
sakshi_net

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial