none
Data Source - Display Member - Value Member for Combo Box

    Question

  • Hi,

     

    I have 2 array lists and 1 combobox:

     

                    ArrayList DisplayMemberList = new ArrayList();

                    DisplayMemberList .Add("Display Member1");
                    DisplayMemberList .Add("Display Member2");


                    ArrayList ValueMemberList = new ArrayList();

                    ValueMemberList .Add("Value Member1");
                    ValueMemberList .Add("Value Member2");

     



                    combobox1.DataSource = DisplayMemberList;

                   
                   
                    int i = 0;

                    for (i = 0; i < DisplayMemberList .Count; i++)
                    {
                        combobox1.DisplayMember = DisplayMemberList [i].ToString();
                    }

                    for (i = 0; i < ValueMemberList.Count; i++)
                    {
                        combobox1.ValueMember = ValueMemberList [i].ToString();
                    }

     

    The above code is throwing an error. can any one help me out in fixing this up.  i need display member to be first array list and value member to be the second array list.


    Thursday, April 28, 2011 6:28 PM

Answers

  • Your display member and value member must be in the same datasource and those properties refer to field names, not the actual data. Scrap your code, it is completely wrong ~sorry~.

    Get the data to bind to your combobox into one collection, eg. a DataTable. If the DataTable had column myID and column myValue you would configure your combobox like so:

    combobox1.DisplayMember = "myValue";
    
    combobox1.ValueMember = "myID";
    
    combobox1.DataSource = myDataTable;
    
    
    
    

    Bob - www.crowcoder.com
    Thursday, April 28, 2011 8:00 PM
  • This code assumes your ArrayList positions match up 1 to 1 - meaning first displaymember goes with first valuemember. If not you must derive a way put the values together correctly.

          ArrayList DisplayMemberList = new ArrayList();
          DisplayMemberList.Add("Display Member1");
          DisplayMemberList.Add("Display Member2");
    
          ArrayList ValueMemberList = new ArrayList();
          ValueMemberList.Add("Value Member1");
          ValueMemberList.Add("Value Member2");
    
          DataTable dt = new DataTable();
          DataColumn displayColumn = new DataColumn("DisplayMemberList", Type.GetType("System.String"));
          DataColumn valueColumn = new DataColumn("ValueMemberList", Type.GetType("System.String"));
          dt.Columns.Add(displayColumn);
          dt.Columns.Add(valueColumn);
    
          Int32 listposition = 0;
          foreach (var item in DisplayMemberList)
          {
            DataRow newrow = dt.NewRow();
            newrow["DisplayMemberList"] = item.ToString();
            newrow["ValueMemberList"] = ValueMemberList[listposition];
            dt.Rows.Add(newrow);
            listposition++;
          }
    
          foreach (DataRow item in dt.Rows)
          {
            Console.Write(item[0]);
            Console.Write(", ");
            Console.Write(item[1] + Environment.NewLine);
          }
          Console.ReadKey();
    

    Bob - www.crowcoder.com
    Thursday, April 28, 2011 8:57 PM

All replies

  • Your display member and value member must be in the same datasource and those properties refer to field names, not the actual data. Scrap your code, it is completely wrong ~sorry~.

    Get the data to bind to your combobox into one collection, eg. a DataTable. If the DataTable had column myID and column myValue you would configure your combobox like so:

    combobox1.DisplayMember = "myValue";
    
    combobox1.ValueMember = "myID";
    
    combobox1.DataSource = myDataTable;
    
    
    
    

    Bob - www.crowcoder.com
    Thursday, April 28, 2011 8:00 PM
  • is there a way that we can manually add these two array lists as columns in a datatable

     

    DataTable dt = new DataTable();

     

    <Code>

    <Code>

    <Code>

     

    dt should have two columns with the values in row by row ( DisplayMemberList, ValueMemberList ) . This should be done without any sql connection. is there any <code> ?


    Thursday, April 28, 2011 8:40 PM
  • This code assumes your ArrayList positions match up 1 to 1 - meaning first displaymember goes with first valuemember. If not you must derive a way put the values together correctly.

          ArrayList DisplayMemberList = new ArrayList();
          DisplayMemberList.Add("Display Member1");
          DisplayMemberList.Add("Display Member2");
    
          ArrayList ValueMemberList = new ArrayList();
          ValueMemberList.Add("Value Member1");
          ValueMemberList.Add("Value Member2");
    
          DataTable dt = new DataTable();
          DataColumn displayColumn = new DataColumn("DisplayMemberList", Type.GetType("System.String"));
          DataColumn valueColumn = new DataColumn("ValueMemberList", Type.GetType("System.String"));
          dt.Columns.Add(displayColumn);
          dt.Columns.Add(valueColumn);
    
          Int32 listposition = 0;
          foreach (var item in DisplayMemberList)
          {
            DataRow newrow = dt.NewRow();
            newrow["DisplayMemberList"] = item.ToString();
            newrow["ValueMemberList"] = ValueMemberList[listposition];
            dt.Rows.Add(newrow);
            listposition++;
          }
    
          foreach (DataRow item in dt.Rows)
          {
            Console.Write(item[0]);
            Console.Write(", ");
            Console.Write(item[1] + Environment.NewLine);
          }
          Console.ReadKey();
    

    Bob - www.crowcoder.com
    Thursday, April 28, 2011 8:57 PM