将字符串值绑定到表示数字字段的ComboBox? [英] Bind string value to ComboBox that represent numeric field?

查看:146
本文介绍了将字符串值绑定到表示数字字段的ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体对象帐户,其中包含以下字段:

I have an entity object "Account" that contain fields such that

public class Account
{
    public string Name{get;set;}
    public int Type{get;set;}
}



帐户类型是固定值,例如 {basic,derived,no type ,...}

我将名称字段绑定到文本框,我想绑定类型到组合框,其中字符串值显示为组合框项目。

我将字典绑定到 ComboBox 这样


Account Type is a fixed values such that {"basic","derived","no type",...}
I bind Name field to text box,I want to bind Type to combo box where string values appear as combo box items.
I bind Dictionary to ComboBox such that

Dictionary<string, int> AccountType = new Dictionary<string, int>
	{
	  {"no type", 0},
	  {"basic", 1},
	  {"derived", 2}
	};
combo.DataSource = new BindingSource(AccountType, null);
combo.DisplayMember = "Key";
combo.ValueMember = "Value";



如何绑定到ComboBox,其中下拉列表仅包含表示Int值的字符串的唯一值。

推荐答案

交换字典格式。或者我误解了这个问题?



Swap the dictionary format. Or I misunderstood the question?

Dictionary<int, string> AccountType = new Dictionary<int, string>
    {
      {0, "no type"},
      {1, "basic"},
      {2, "derived"}
    };
combo.DataSource = new BindingSource(AccountType, null);
combo.DisplayMember = "Key";
combo.ValueMember = "Value";


这篇关于将字符串值绑定到表示数字字段的ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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