C#Winforms-设置组合框选定的值 [英] C# Winforms - setting combobox selected value

查看:132
本文介绍了C#Winforms-设置组合框选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为组合框设置名称和值对。因此,我创建了一个名为 Item 的类,如下所示:

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;
    }
}

数据集如下:

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;

这是输出:

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

在我的示例必须选择非洲大陆,但不是。
不仅如此,例如,这段代码还从 person 表中获取数据:

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);

当我编写 comboBox2.SelectedValue = a.continental 时,必须选择非洲大陆,但不是。我没有解决问题。

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

推荐答案

SelectedValue 物业文档:

As described in the SelectedValue property documentation:


属性值

包含成员值的对象 ValueMember 属性。

注释

如果未在 ValueMember ,SelectedValue返回 ToString的结果对象的方法。

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

要获得所需的行为,您需要公开 Name Value 作为 Item 类的公共属性,并利用控件的DataSource ValueMember DisplayMember 属性:

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;
    }
}

和示例用法:

// 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;

这篇关于C#Winforms-设置组合框选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆