简体   繁体   English

ComboBox和KeyValuePair列表无法正常工作

[英]ComboBox and KeyValuePair List not working correctly

I have created a ComboBox on a Windows Forms form. 我在Windows窗体表单上创建了一个ComboBox。 It is Databound to a column in a TableAdapter and the DataSource is a manually created KeyValuePair List. 它是数据绑定到TableAdapter中的列,而DataSource是手动创建的KeyValuePair列表。 The problem is that when the form is displayed; 问题是当表格显示时; the ValueMember is displayed in the ComboBox, not the DisplayMember. ValueMember显示在ComboBox中,而不是DisplayMember中。 If you click the drop down, the Key List Values are displayed. 如果单击下拉列表,将显示“键列表值”。 When you make a selection, in the OnValidating method, the SelectedItem is -1. 在进行选择时,在OnValidating方法中,SelectedItem为-1。

I believe that the ComboBox is setup correctly. 我相信ComboBox设置正确。 What have I done wrong? 我做错了什么?

this.cboFormat.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.BindingSource, "Format", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

InitializeComponent();

List<KeyValuePair<int, string>> loFormat = new List<KeyValuePair<int, string>>();
loFormat.Add(new KeyValuePair<int, string>(1, "Format 1"));
loFormat.Add(new KeyValuePair<int, string>(2, "Format 2"));
loFormat.Add(new KeyValuePair<int, string>(3, "Format 3"));

this.cboFormat.DataSource = new BindingSource(loFormat, null);
this.cboFormat.DisplayMember = "Value";
this.cboFormat.ValueMember = "Key";

Problem solved: 问题解决了:

I found that if the Column in the DataBinding is an int, but the Value from the List is a string, then the problem above resulted. 我发现如果DataBinding中的Column是一个int,但List中的Value是一个字符串,那么上面的问题就会产生。 I changed the Databinding to a view that displayed the text from the lookup table the int tied to. 我将数据绑定更改为一个视图,该视图显示了与int绑定的查找表中的文本。 The Key of the List would be the int from the lookup table. 列表的键将是查找表中的int。 If that makes any sense. 如果那有意义的话。

SELECT DisplayMember FROM LookupTable AS LT INNER JOIN DataTable AS DT ON LT.Id = DT.LookupId.

Then the KeyValuePair worked as expected. 然后KeyValuePair按预期工作。

I threw together a window with a ComboBox and a Button. 我把一个带有ComboBox和Button的窗口放在一起。 I don't see what you describe at all. 我根本没有看到你描述的内容。 My code looks something like this: 我的代码看起来像这样:

    BindingSource bs = new BindingSource();
    public Form1()
    {
        InitializeComponent();
        comboBox1.DataBindings.Add(new Binding("Text", bs, "Format", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
        List<KeyValuePair<int, string>> loFormat = new List<KeyValuePair<int, string>>();
        loFormat.Add(new KeyValuePair<int, string>(1, "Format 1"));
        loFormat.Add(new KeyValuePair<int, string>(2, "Format 2"));
        loFormat.Add(new KeyValuePair<int, string>(3, "Format 3"));
        comboBox1.DataSource = new BindingSource(loFormat, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
    }

    private void comboBox1_Validating(object sender, CancelEventArgs e)
    {
        Console.WriteLine(comboBox1.SelectedItem);
    }

The validating handler has the correct selected item and it initializes just fine. 验证处理程序具有正确的选定项目,并初始化就好了。 Maybe it is something else that you haven't show in your code snippet. 也许这是你没有在你的代码片段中显示的其他内容。

Why are you using a List<KeyValuePair<int,string>> instead of a Dictionary<int,string> ? 为什么使用List<KeyValuePair<int,string>>而不是Dictionary<int,string>

Anyway, I do believe your problem is located in the fact that since the binding is done against a List of KeyValuePair; 无论如何,我确实认为你的问题在于,因为绑定是针对KeyValuePair的List完成的; the code will think that it should look for the values of DisplayMember in the List (that is, not the KeyValuePair) and finds the Value being the KeyValue and doesn't find any Key, so it does some odd parsing (READ: not sure about why you get a -1). 代码会认为它应该在List中查找DisplayMember的值(即不是KeyValuePair),并且发现Value是KeyValue并且找不到任何Key,所以它做了一些奇怪的解析(READ:不确定)为什么你得到-1)。 Think switching from List<KeyValuePair<int,string>> to a dictionary might just solve it. 想想从List<KeyValuePair<int,string>>切换到字典可能只是解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM