设置组合框选定值

设置组合框选定值

本文介绍了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.

要获得所需的行为,您需要将 NameValue 作为您的 Item 类的公共属性公开并利用 控件的DataSourceValueMemberDisplayMember 属性:

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 - 设置组合框选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:36