Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

when is ComboBox.SelectedValue not null?

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
trying to set the value of a ComboBox programmatically.
the list contains custom classes with the DisplayMember and ValueMember properties set correctly.
unfortunately, SelectedValue is always null.
is there something i'm missing?

Code:
public class ValuePair
{
 // these are properties really
 public string KeyValue;
 public string DescriptionValue;
 ValuePair(string k, string d)
 {
  KeyValue = k;
  DescriptionValue = d;
 }
}

class MyForm // edited for brevity
{
private void MyForm_Load(object sender, EventArgs e)
{
 comboBox1.ValueMember = "KeyValue";
 comboBox1.DisplayMember = "DescriptionValue";
 comboBox1.Items.Add(new ValuePair("A", "Value A");
 comboBox1.Items.Add(new ValuePair("B", "Value B");
 comboBox1.Items.Add(new ValuePair("C", "Value C");
 comboBox1.Items.Add(new ValuePair("D", "Value D");
 comboBox1.Items.Add(new ValuePair("E", "Value E");
}

private void button1_Click(object sender, EventArgs e)
{
 // comboBox1.SelectedValue == null

 comboBox1.SelectedValue = "B";

 // comboBox1.SelectedValue == null // still!
}
}

mr s. <;)

 
or rather, what on earth does "SelectedItem.Tag" mean?

anyhoo, i think i found the reason why it doesn't work, or a hint at least.

from this random page i get the following:

[tt]Setting the System.Windows.Forms.ListControl.SelectedValue property works only if the control is data bound.[/tt]

the page for ListControl.SelectedValue makes no mention of this.

and i thought i was databinding the control, but there you go.

so, my question remains.
i have a combobox which is bound to a list of custom objects and uses the DisplayMember and ValueMember properties.
if i have a value that i want to use to set the value on this comboBox, which property or method do i use?

Code:
comboBox1.WhichProperty = newValue;
// or even
comboBox1.WhichMethod(newValue);

many thanks,

mr s. <;)

 
when they say databound, they mean that the DataSource property is not null.

if you use a list object outside of the control and point the DataSource property at it, the following works:

Code:
comboBox1.SelectedValue = newValue;

if, on the other hand, you use the internal list, it doesn't.

how dumb is that?


mr s. <;)

 
The way I'd use it

Code:
  ((ValuePair) comboBox1.SelectedItem).KeyValue = newVal;

------------------
When you do it, do it right.
 
Instead of adding individual items to a combo box, couldn't you just create a generic collection of objects that would expose your value and display text?

Then you can merely set your datasource to the collection and display and value text to the relevant properties of the object.

I guess the main difference is that you are adding items to the collection rather than the combo box.
 
dEVooXiAm: i don't want to set the value of the item in the collection, i just want to set the comboBox to display the correct item in the list.

GoTerps88: that's pretty a cool idea. i'll try it and get back to you.

many thanks,




mr s. <;)

 
Something like this:

Code:
public static IEnumerable getCategories()
    {
        string cmd = "proc_sel_Categories";
        List<Categories> categorylist;
        SqlDataReader objReader;
        Categories c;
        try
        {

            using (SqlConnection objConn = new SqlConnection(GetConnectionString()))
            {

                using (SqlCommand objCmd = SqlCommand(cmd, objConn))
                {
                    objCmd.CommandType = CommandType.StoredProcedure;
                    
                    objConn.Open();
                    using (objReader = objCmd.ExecuteReader(
                        CommandBehavior.CloseConnection))
                    {

                        categorylist = new List<Categories>();

                        while (objReader.Read())
                        {
                            c = new Categories();
                            c.UCatid = objReader.GetInt32(0);
                            c.Category = objReader.GetString(1);
                            categorylist.Add(c);
                        }
                    }
                    
                    return categorylist;

                }
            }


        }
Code:
public class Categories
{
    private int ucatid;
    private string category;

    public Categories()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int UCatid
    {
        get
        {
            return ucatid;
        }
        set
        {
            ucatid = value;
        }
    }
    public string Category
    {
        get
        {
            return category;
        }
        set
        {
            category = value;
        }
    }
}
Code:
      cbo.DataSource = class.getCategories();
      cbo.ValueMember = "UCatId";
      cbo.DisplayMember = "Category";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top