我不知道我的頭腦是不是今天工作,或者如果這實際上比我認爲應該更難。如何在綁定的DataGridViewComboBoxCell中獲取對基礎對象的引用?
我有一個DataGridView DataGridViewComboBoxColumn綁定到一個通用的IList的CustomObjects。以下是我如何設置列的粗略代碼示例。
DataGridViewComboBoxColumn location = new DataGridViewComboBoxColumn()
{
Name = "Location",
DataSource = new BindingSource(GetMyLocations(), null),
DisplayMember = "Name",
ValueMember = "ID"
};
然後,當我想返回一個單元格的值:
string id = (string)row.Cells["Location"].Value;
但我不希望將ID屬性的引用,我想實際的位置對象的引用!我試過沒有指定一個ValueMember,但似乎只是使用DisplayMember。
我可以迭代列DataSource中的所有Location對象並檢查匹配的ID屬性,但是,它似乎像ComboBoxCell應該能夠直接返回。
此外,我不能在這種情況下使用字典。
任何想法,以實現它的最佳方式?