?
Solved

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

Posted on 2007-08-06
10
Medium Priority
?
625 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:jackjohnson44
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 12

Expert Comment

by:gbzhhu
ID: 19639487
Use InsertAt method of the combobox after it is bound to add values
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19639506
Guid.Empty is the usual representation for None.  It represents this value:

"00000000-0000-0000-0000-000000000000"

Bob
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 19639508
See this

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

basically you do

ComboBox1.Items.InsertAt(0, "All")
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 8

Expert Comment

by:sabeesh
ID: 19639511
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);
0
 

Author Comment

by:jackjohnson44
ID: 19639662
this is a windows applicaiton, isn't listbox only a web collection?  What namespace do I have to use?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19639765
ListItem is an ASP.NET class.  You need System.Windows.Forms.ListBox.

Bob
0
 

Author Comment

by:jackjohnson44
ID: 19639863
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);
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19639883
Add the items to the data source, and refresh the ComboBox.

Bob
0
 

Author Comment

by:jackjohnson44
ID: 19640014
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.
0
 
LVL 2

Accepted Solution

by:
sakshi_net earned 1500 total points
ID: 19640670
I have one comboBox called comboBox1 ...
after binding it to database .. You can insert item like below :

            this.comboBox1.Items.Insert(0, "All");
            this.comboBox1.Items.Insert(1, "None");
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 1 hour left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question