Solved

Display and Value member binding to a ComboBox

Posted on 2006-11-27
1
1,159 Views
Last Modified: 2007-12-19
Hello,
I am trying to bind the display and value members to a combobox from an arrayList that contains a custon Object.

I made the object "ComboItem" that holds a name and a value.  Each of these are added to the Arraylist.  The datasource line works, but it won't accept the names of the DisplayMember or the ValueMember.

What am I doing wrong?  I've already tried changing the value variable in ComboItem as an int; and changing the case of the names.

thanks,
Sarah


 private void MainForm_Load(object sender, EventArgs e) {      
          IList aList = new ArrayList();
            ComboItem cItemNone = new ComboItem("None", "-1");
       
            aList.Add(cItemNone);

            foreach (DataRow dr in dataSet.Tables["Students"].Rows) {
                ComboItem cItem = new ComboItem(dr[1].ToString(),dr[0].ToString());
                aList.Add(cItem);
            }

            StudentComboBox.DataSource = aList;

            StudentComboBox.DisplayMember = "name";
            StudentComboBox.ValueMember = "value";
   }



 private class ComboItem {
            public string Name;
            public string Value;

            public ComboItem(string name, string value) {
                Name = name;
                Value = value;
            }
        }
0
Comment
Question by:sarahjackel
1 Comment
 
LVL 8

Accepted Solution

by:
sabeesh earned 500 total points
ID: 18025313
Use this class istead of your class

  public class ComboItem
    {
        int _value = 0;
        string _text = string.Empty;

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
       
    }
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now