locked
set combobox.datasource to string array RRS feed

  • Question

  • hi, i have a simple string array that i databind with a combo box, what would i set for the displaymember and valuemember, because without setting the valuemember i can not change the selectedvalue, and when the combobox is databond the selecteditem dont work. am I left with no option but to manually fill the combobox.items property?   
    Monday, July 11, 2011 7:39 PM

Answers

  • Is this a simple array of strings or an array of objects? Why do you have displaymember/valuemember set when there is only one item in the selection at a time? According to you a "String" 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          String[] myArray = { "Barlow", "Ben", "Susan", "Straker", "Salem's Lot" };
          comboBox1.DataSource = myArray;
          comboBox1.SelectedValueChanged += new EventHandler(this.comboBox1_SelectedValueChanged);
        }
    
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
          MessageBox.Show(comboBox1.SelectedItem.ToString());
        }
      }
    }
    



    John Grove, MCC - Senior Software Engineer

    • Proposed as answer by Molku Monday, July 11, 2011 8:29 PM
    • Marked as answer by Andreas Johansson Wednesday, July 13, 2011 9:53 AM
    Monday, July 11, 2011 7:48 PM

All replies

  • See here
    John Grove, MCC - Senior Software Engineer
    • Proposed as answer by Adam_Turner Monday, July 11, 2011 7:47 PM
    Monday, July 11, 2011 7:41 PM
  • thanks for the reply, but please understand the question before you reply. or ask if i am not clear on the question.
    Monday, July 11, 2011 7:46 PM
  • Is this a simple array of strings or an array of objects? Why do you have displaymember/valuemember set when there is only one item in the selection at a time? According to you a "String" 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          String[] myArray = { "Barlow", "Ben", "Susan", "Straker", "Salem's Lot" };
          comboBox1.DataSource = myArray;
          comboBox1.SelectedValueChanged += new EventHandler(this.comboBox1_SelectedValueChanged);
        }
    
        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
          MessageBox.Show(comboBox1.SelectedItem.ToString());
        }
      }
    }
    



    John Grove, MCC - Senior Software Engineer

    • Proposed as answer by Molku Monday, July 11, 2011 8:29 PM
    • Marked as answer by Andreas Johansson Wednesday, July 13, 2011 9:53 AM
    Monday, July 11, 2011 7:48 PM
  • Or if you have a Dictionary

    Dictionary<Int32, String> risk = new Dictionary<int, string>();
    risk.Add(1, "Low");
    risk.Add(2, "Medium");
    risk.Add(3, "High");
    comboBox1.DataSource = new BindingSource(risk, null);
    comboBox1.DisplayMember = "Value";
    comboBox1.ValueMember = "Key";
    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
    
    
    
    



    John Grove, MCC - Senior Software Engineer
    • Proposed as answer by Mitja Bonca Monday, July 11, 2011 8:08 PM
    Monday, July 11, 2011 7:54 PM
  • As John post an example (two above, with a string array example), you cannot set ValueMember there. But if you use a Dictionary, whihc has Key and Value(s), you can set Key as ValueMemeber and Value and DisplayMember.

    btw, greate example John.


    Mitja
    Monday, July 11, 2011 8:08 PM
  • i see that you didnt get my question, you over complicated.  its really simple, i bind a combobox to a string array, and i want to change the combobox.valuemember. but i can not do that unless i set the valuemember. but what should i set the valuemember?  so is my only option is to manually fill the combobox.items with my string[]?

     

    string[] a = new string[]{"a", "b"};

    combobox.datasource = a;

    combobox.displaymember = ?

    combobox.valuemember = ?

    Monday, July 11, 2011 8:16 PM
  • You don't "change" the ValueMember. The ValueMember is when you are binding to an object that has two properties, one for viewing and one for its value.

    John Grove, MCC - Senior Software Engineer
    Monday, July 11, 2011 8:24 PM
  • i see that you didnt get my question, you over complicated.  its really simple, i bind a combobox to a string array, and i want to change the combobox.valuemember. but i can not do that unless i set the valuemember. but what should i set the valuemember?  so is my only option is to manually fill the combobox.items with my string[]?

     

    string[] a = new string[]{"a", "b"};

    combobox.datasource = a;

    combobox.displaymember = ?

    combobox.valuemember = ?

    Didnt you read what I wrote? Read it three times.

    This is impossible - you cannot set ValueMember with just using string array. From where? Think a little; you dont have any other thing as buch of string in array. No values at all.

    To do a work around, you can use a Dictionary, as John showed a nice example.

     


    Mitja
    Monday, July 11, 2011 8:26 PM
  • John's sample code is correct. Notice how it uses SelectedItem instead of SelectedValue.
    Bob - www.crowcoder.com
    Monday, July 11, 2011 8:31 PM
  • sorry, i mean selectedvalue. but you guys r awesome, the BEST help ever.  

    Monday, July 11, 2011 8:38 PM
  • Again, you don't change the value, you "get" the value like this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          Dictionary<Int32, String> risk = new Dictionary<Int32, String>();
          risk.Add(1, "Low");
          risk.Add(2, "Medium");
          risk.Add(3, "High");
          comboBox1.DataSource = new BindingSource(risk, null);
          comboBox1.DisplayMember = "Value";
          comboBox1.ValueMember = "Key";
          comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
          comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          Int32 riskValue = Convert.ToInt32(comboBox1.SelectedValue);
          MessageBox.Show(riskValue.ToString());
      
        }
      }
    }
    
    



    John Grove, MCC - Senior Software Engineer
    Monday, July 11, 2011 8:46 PM
  • In simpler terms, the DisplayMember/ValueMember is for displaying one thing but getting another when an item is selected.
    John Grove, MCC - Senior Software Engineer
    Monday, July 11, 2011 9:07 PM