Solved

Display and Value member binding to a ComboBox

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

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…
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.

832 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