Solved

Display and Value member binding to a ComboBox

Posted on 2006-11-27
1
1,160 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

20 Experts available now in Live!

Get 1:1 Help Now