C# Winforms - setting combobox selected value

做~自己de王妃 提交于 2019-12-11 00:37:40

问题


I want to set name and value pairs for combobox. So I created a class named Item like this:

// Content item for the combo box
private class Item
{
    private readonly string Name;
    private readonly int Value;
    private Item(string _name, int _value)
    {
        Name = _name; Value = _value;
    }
    private override string ToString()
    {
        // Generates the text shown in the combo box
        return Name;
    }
}

And data set like this:

comboBox1.DataSource = null;
comboBox1.Items.Clear();
// For example get from database continentals.
var gets = _connection.Continentals;
comboBox1.Items.Add(new Item("--- Select a continental. ---", 0));
foreach (var get in gets)
{
    comboBox1.Items.Add(new Item(get.name.Length > 40 ? get.name.Substring(0, 37) + "..." : get.name, Convert.ToInt32(get.id)));
}
// It points Africa.
comboBox1.SelectedValue = 3;

Here is the output:

// 1 - Europe
// 2 - Asia
// 3 - Africa
// 4 - North America
// 5 - South America
// 6 - Australia
// 7 - Antartica

In my example The Africa continental must be selected but it is not. And more than that in my edit form, for example this code gets datas from person table:

var a = _connection.persons.SingleOrDefault(x => x.id == Id);

When I code comboBox2.SelectedValue = a.continental, the Africa continental must be selected, but it is not. I did not solve the problem.


回答1:


As described in the SelectedValue property documentation:

Property Value
An object containing the value of the member of the data source specified by the ValueMember property.

Remarks
If a property is not specified in ValueMember, SelectedValue returns the results of the ToString method of the object.

To get the desired behavior, you need to expose Name and Value as public properties of your Item class and utilize the DataSource, ValueMember and DisplayMember properties of the control:

// Content item for the combo box
private class Item
{
    public string Name { get; private set; }
    public int Value { get; private set; }
    private Item(string _name, int _value)
    {
        Name = _name; Value = _value;
    }
}

and the sample usage:

// Build a list with items
var items = new List<Item>();
// For example get from database continentals.
var gets = _connection.Continentals;
items.Add(new Item("--- Select a continental. ---", 0));
foreach (var get in gets)
{
    items.Add(new Item(get.name.Length > 40 ? get.name.Substring(0, 37) + "..." : get.name, Convert.ToInt32(get.id)));
}
// Bind combobox list to the items
comboBox1.DisplayMember = "Name"; // will display Name property
comboBox1.ValueMember = "Value"; // will select Value property
comboBox1.DataSource = item; // assign list (will populate comboBox1.Items)

// Will select Africa
comboBox1.SelectedValue = 3;


来源:https://stackoverflow.com/questions/41944146/c-sharp-winforms-setting-combobox-selected-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!