繁体   English   中英

绑定字典 <int, auto-property> 作为组合框的数据源

[英]Binding a dictionary<int, auto-property> as datasource to a combobox

今天,在将数据源绑定到具有自动属性的字典时,我遇到了一个奇怪的问题。

  • IDE:Visual Studio 2015
  • 语言:C#
  • 用法:Windows窗体应用程序

我创建了一个字典,将Auto属性作为值:

字典:

/// <summary>
/// Class that cotains names of various DMS items of the currently selected batch.
/// </summary>
internal class DMSItemDictionary : Dictionary<int, DMSItemName>
{
    /// <summary>
    /// Adds a DMS item to the dictionary.
    /// </summary>
    /// <param name="ItemKey">Key of the DMS item.</param>
    /// <param name="ItemID">ID of the DMS item.</param>
    /// <param name="ItemName">Name of the DMS item.</param>
    internal void Add(int ItemKey, int ItemID, string ItemName)
    {
        DMSItemName DMSItem = new DMSItemName(); // Creates a new DMS item auto property.
        DMSItem.ID = ItemID; // Sets the item ID.
        DMSItem.Name = ItemName; // Sets the item name.
        this.Add(ItemKey, DMSItem); // Adds the DMS item to the dictionary.
    }
}

汽车属性:

/// <summary>
/// Auto property that's used for DMS item names.
/// </summary>
internal class DMSItemName
{
    /// <summary>
    /// The ID of the DMS item.
    /// </summary>
    public int ID { get; set; }

    /// <summary>
    /// The name of the DMS item.
    /// </summary>
    public string Name { get; set; }
}

我使用字典中的整数作为可能性,以从其他装配中获取项目的顺序对项目进行排序。

这是一个示例,我如何用条目填充字典。

示例条目插入:

Dictionaries.DMSItemDictionary DMSIndexComboBoxData = new Dictionaries.DMSItemDictionary();
for (int IndexCount = 0; IndexCount < DMSIndizes.Count; IndexCount++)
{
    int IndexID = DMSIndizes[IndexCount].ID;
    string IndexName = DMSIndizes[IndexCount].Name;
    DMSIndexComboBoxData.Add(IndexCount, IndexID, IndexName);
}

这是我如何绑定字典的示例。

数据绑定示例:

BindingSource DataSource = new BindingSource(DMSIndexComboBoxData, null);
DataGridViewComboBoxCell comboBoxIndizes = new DataGridViewComboBoxCell();
comboBoxIndizes.DisplayMember = "Value.Name";
comboBoxIndizes.ValueMember = "Value.ID";
comboBoxIndizes.DataSource = DataSource;

我的问题是,组合框中仅显示字典中的第一个条目。 除了一切正常之外(正确的字段用作显示成员,正确的字段用作值成员)。

另外,如果我使用foreach和控制台遍历字典,则所有条目均会正确返回。

谁能找到我有这个问题的原因?

提前非常感谢Florian'kami'Zedler

PS对不起,我的英语不好,如果您有任何问题,我会尝试进一步解释。

进一步说明:字典由功能从另一个程序集中以指定顺序填充。 一些示例数据:

  • 物品ID:10203,物品名称:Test1234
  • 项目ID:0815,项目名称:测试
  • ItemID:1,ItemName:Test1
  • 物品ID:99999,物品名称:qwerty

我的客户希望该订单被撤销。 (最后输入是第一个,但不是按ID排序)这就是我具有此字典/自动性能设置的原因,在这里我可以简单地更改键的顺序。

如果只想反转顺序,为什么不使用Array Reverse方法呢? 样例代码:

DMSItemName[] DMSIndizes = new DMSItemName[4];

//Sample data
DMSIndizes[0] = new DMSItemName();
DMSIndizes[0].ID = 10203;
DMSIndizes[0].Name = "Test1234";
DMSIndizes[1] = new DMSItemName();
DMSIndizes[1].ID = 0815;
DMSIndizes[1].Name = "Test";
DMSIndizes[2] = new DMSItemName();
DMSIndizes[2].ID = 1;
DMSIndizes[2].Name = "Test1";
DMSIndizes[3] = new DMSItemName();
DMSIndizes[3].ID = 99999;
DMSIndizes[3].Name = "qwerty";

var reversed = DMSIndizes.Reverse();
BindingSource DataSource = new BindingSource(reversed, null);
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Id";  
comboBox1.DataSource = DataSource;

暂无
暂无

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

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