today I'm having a strange problem with binding a datasource to a dictioary with a autoproperty.
I have created a dictionary with a Auto property as value:
Dictionary:
/// <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.
}
}
Auto property:
/// <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; }
}
I'm using the interger in the dictionary as posibility to sort the items in the order I get them from another assembly.
Here is an example how I fill the dictionary with entries.
Example entry insert:
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);
}
And here is an example how I bind the dictionary.
Example data bind:
BindingSource DataSource = new BindingSource(DMSIndexComboBoxData, null);
DataGridViewComboBoxCell comboBoxIndizes = new DataGridViewComboBoxCell();
comboBoxIndizes.DisplayMember = "Value.Name";
comboBoxIndizes.ValueMember = "Value.ID";
comboBoxIndizes.DataSource = DataSource;
My problem is that only the first entry from the dictionary is shown in the combobox. Except that everything works fine (The correct field is used as display member and the correct field is used as value member.)
Addionally if I loop through the dictionary with a foreach and a console write all entries are returned correctly.
Can anyone find a reason why I have this Problem?
Many thanks in advance, Florian 'kami' Zedler
P.S. Sorry for my bad English, if you have any questions I try to explain even further.
Further explanation: The Dictionary gets populatet by a function from another assembly in a specified order. Some Example Data:
My customer wants that order to be reversed. (Last entry first, but not ordered by ID) Thats the reason why I have this dictionary/autoporperty setup, here I can simply change the order of the key.
If you just want to reverse the order, why not using Reverse
method of Array
? Sample code:
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;