Click here to Skip to main content
14,868,300 members
Articles / Programming Languages / C#
Article
Posted 7 Oct 2006

Stats

162.8K views
1.8K downloads
52 bookmarked

Add Item to Binded Combo Box

Rate me:
Please Sign up or sign in to vote.
4.62/5 (17 votes)
7 Oct 2006CPOL1 min read
This article shows how a new item can be added to a data binded combo box

Problem Scenario

The Windows Forms ComboBox control is used to display data in a drop-down combo box. When the data source property of the combo box is binded with data, then you will not be able to add any item through the Items.Add() function.

So, let's go through a demo by the help of which you can understand the problem scenario and its solution. Suppose we have a class named Person and we will create an IList to store this Person list.

C#
Person p = null;
IList list = new ArrayList();

p = new Person();
p.PersonID = 1;
p.PersonName = "Ali";
list.Add(p);

p = new Person();
p.PersonID = 2;
p.PersonName = "Raza";
list.Add(p);

p = new Person();
p.PersonID = 3;
p.PersonName = "Shaikh";
list.Add(p);

We have added three persons information in the list, now we want to bind the combo box with this data.

C#
this.comboBox1.DataSource = list;
this.comboBox1.DisplayMember = "PersonName";
this.comboBox1.ValueMember = "PersonID";

Now, the data is binded with the Combo Box, and we will see the following kind of look on the interface.

Image 1

Now, what's the problem here? In most of the professional applications, there is a select option at the top of each combo box, so that the user can choose an option if he desires, or else he can leave the combo box without selecting anything. So, as a common practice, one will write this line of code:

C#
this.comboBox1.Items.Add("< Select Person >");
//OR
this.comboBox1.Items.Insert(0, "< Select Person >");

Which ends up giving this error:

Image 2

Solution

This thing can be achieved by using the following two classes of System.Reflection:

  • Activator: Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects
  • PropertyInfo: Discovers the attributes of a property and provides access to property metadata.

Now, simply call this function to add the required item on the list.

C#
private void AddItem(IList list, Type type, string valueMember, 
	string displayMember, string displayText)
{
            //Creates an instance of the specified type 
            //using the constructor that best matches the specified parameters.
            Object obj = Activator.CreateInstance(type);

            // Gets the Display Property Information
            PropertyInfo displayProperty = type.GetProperty(displayMember);

            // Sets the required text into the display property
            displayProperty.SetValue(obj, displayText, null);

            // Gets the Value Property Information
            PropertyInfo valueProperty = type.GetProperty(valueMember);

            // Sets the required value into the value property
            valueProperty.SetValue(obj, -1, null);

            // Insert the new object on the list
            list.Insert(0, obj);
}

Then, add the following line of code just before the data binding with the combo box:

C#
AddItem(list, typeof(Person), "PersonID", "PersonName", "< Select Option >");

In the end, the interface looks like this, looking like a professional effort to deal with the data inside the combo box.

Image 3

History

  • 7th October, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ali Raza Shaikh
Software Developer
Pakistan Pakistan
Ali Raza Shaikh did his BS (CS) from FAST-NUCES, Karachi Campus. He is also a Microsoft Certified Application Developer. He have a great passion for programming and all the programming related stuff. He can be reached at alirazashaikh.blogspot.com

Comments and Discussions

 
Questionvar ? Pin
komperpiet11-Oct-12 23:43
Memberkomperpiet11-Oct-12 23:43 
AnswerRe: var ? Pin
kerrymarke30-Oct-14 19:59
Memberkerrymarke30-Oct-14 19:59 
GeneralMy vote of 4 Pin
Kanasz Robert27-Sep-12 10:51
professionalKanasz Robert27-Sep-12 10:51 
GeneralHmmm..... Pin
Jason S. Rodgers7-Dec-08 10:15
MemberJason S. Rodgers7-Dec-08 10:15 
QuestionPassing DataRow Pin
ariel shtul29-Jul-08 23:34
Memberariel shtul29-Jul-08 23:34 
GeneralVisual Basic 2005 ComboBox Pin
Arturol28-Mar-08 4:59
MemberArturol28-Mar-08 4:59 
GeneralHi Pin
bsundarapandian3-Jan-07 21:14
Memberbsundarapandian3-Jan-07 21:14 
GeneralRe: Hi Pin
Ali Raza Shaikh17-Jan-07 16:12
MemberAli Raza Shaikh17-Jan-07 16:12 
QuestionSimpler solution? Pin
Tom the Coder12-Oct-06 4:21
MemberTom the Coder12-Oct-06 4:21 
AnswerRe: Simpler solution? Pin
Ali Raza Shaikh12-Oct-06 4:38
MemberAli Raza Shaikh12-Oct-06 4:38 
GeneralRe: Simpler solution? Pin
GMAinChico19-Dec-06 13:30
MemberGMAinChico19-Dec-06 13:30 
GeneralRe: Simpler solution? Pin
BrunoMorais7-May-07 4:08
MemberBrunoMorais7-May-07 4:08 
AnswerRe: Simpler solution? Pin
Ali Raza Shaikh12-Oct-06 4:45
MemberAli Raza Shaikh12-Oct-06 4:45 
p = new Person();
p.PersonID = -1;
p.PersonName = "";
list.Insert(0, p);

This code is correct, but still it will give error as mentioned in the article if you want to do this after binding the datasource, because once the control is data binded, no item can be added afterward, so by using the reflection properties, you can add more items later.



Ali Raza Shaikh
http://pkblogs.com/alirazashaikh

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.