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.
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.
Use InsertAt method of the combobox after it is bound to add values
Guid.Empty is the usual representation for None. It represents this value:
"00000000-0000-0000-0000-0 0000000000 0"
Bob
"00000000-0000-0000-0000-0
Bob
See this
http://www.thescripts.com/forum/thread165843.html
basically you do
ComboBox1.Items.InsertAt(0 , "All")
http://www.thescripts.com/forum/thread165843.html
basically you do
ComboBox1.Items.InsertAt(0
pComboName.DataSource = oDataHelper.moDataSet.Tabl es[0];
pComboName.DataTextField = pDataTextField;
pComboName.DataValueField = pDataValueField;
pComboName.DataBind();
ListItem listItem = new ListItem();
listItem.Text = "All";
listItem.Value = "0";
pComboName.Items.Insert(0, listItem);
pComboName.DataTextField = pDataTextField;
pComboName.DataValueField = pDataValueField;
pComboName.DataBind();
ListItem listItem = new ListItem();
listItem.Text = "All";
listItem.Value = "0";
pComboName.Items.Insert(0,
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.ListB ox.
Bob
Bob
ASKER
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.DisplayMem ber = m_ds.TestPlanCodes.TestPla nCodeColum n.ColumnNa me;
cmbTestPlanCode.ValueMembe r = m_ds.TestPlanCodes.TestPla nCodeColum n.ColumnNa me;
ListBox lb = new ListBox();
lb.Text = "ALL";
lb.ValueMember = "0";
cmbTestPlanCode.Items.Inse rt(0, lb);
"Items collection cannot be modified when the DataSource property is set."
cmbTestPlanCode.DataSource
cmbTestPlanCode.DisplayMem
cmbTestPlanCode.ValueMembe
ListBox lb = new ListBox();
lb.Text = "ALL";
lb.ValueMember = "0";
cmbTestPlanCode.Items.Inse
Add the items to the data source, and refresh the ComboBox.
Bob
Bob
ASKER
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-0 0000000000 0", 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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.