无法从绑定到 LINQ 的 CheckedListBoxControl 获取 Value 和 DisplayMember
Can't get Value and DisplayMember from CheckedListBoxControl bound to LINQ
之前在 Whosebug 上有人问过这个问题,所以你可能认为它是重复的,但我已经尝试了很多解决方案,但我仍然卡住了。
我有一个绑定到 LINQ 查询的 WinForms CheckedListBoxControl,但我无法获取值和 DisplayMembers。
下面是获取 Value 和 DisplayMember 值的一些尝试:
var avail = from c in dc.CostCenters
select new { Item = c.CostCenterID,
Description = c.CostCenterID + ": " + c.Description };
myList.DataSource = avail;
myList.DisplayMember = "Description";
//Retrieval:
foreach (var item in myList.CheckedItems)
{
DataRowView row = item as DataRowView; //Try 1: row is empty
string displayMember = item["Description"]; //Try 2: Cannot apply indexing with [] to an expression of type 'object'
var x = item[0]; //Try 3: Cannot apply indexing with [] to an expression of type 'object'
row3 = ((DataRowView)myList.CheckedItems[item]).Row; //Try 5 million: Compile error - invalid arguments
}
假设您只想 get/display 您的 ValueMembers/DisplayMembers:
根据您提供的示例,我假设您从 linq 查询中获得了 IEnumerable<dynamic>
。我已将我的测试场景转换为 List<dynamic>
:
List<dynamic> list = new List<dynamic>
{
new { Item = 1, Description = "1: Item1"},
new { Item = 2, Description = "2: Item2"}
};
只需将 BindingSource 添加到 CheckedListBox:
BindingSource bindingSource = new BindingSource(list, null);
checkedListBox1.DataSource = bindingSource;
checkedListBox1.ValueMember = "Item";
checkedListBox1.DisplayMember = "Description";
当您选择了项目并想要查看时:
var checkedItems = checkedListBox1.CheckedItems;
foreach (dynamic checkedItem in checkedItems)
{
Console.WriteLine("valuemember: " + checkedItem.Item);
// or whatever code you have
}
希望对您有所帮助!
之前在 Whosebug 上有人问过这个问题,所以你可能认为它是重复的,但我已经尝试了很多解决方案,但我仍然卡住了。
我有一个绑定到 LINQ 查询的 WinForms CheckedListBoxControl,但我无法获取值和 DisplayMembers。
下面是获取 Value 和 DisplayMember 值的一些尝试:
var avail = from c in dc.CostCenters
select new { Item = c.CostCenterID,
Description = c.CostCenterID + ": " + c.Description };
myList.DataSource = avail;
myList.DisplayMember = "Description";
//Retrieval:
foreach (var item in myList.CheckedItems)
{
DataRowView row = item as DataRowView; //Try 1: row is empty
string displayMember = item["Description"]; //Try 2: Cannot apply indexing with [] to an expression of type 'object'
var x = item[0]; //Try 3: Cannot apply indexing with [] to an expression of type 'object'
row3 = ((DataRowView)myList.CheckedItems[item]).Row; //Try 5 million: Compile error - invalid arguments
}
假设您只想 get/display 您的 ValueMembers/DisplayMembers:
根据您提供的示例,我假设您从 linq 查询中获得了 IEnumerable<dynamic>
。我已将我的测试场景转换为 List<dynamic>
:
List<dynamic> list = new List<dynamic>
{
new { Item = 1, Description = "1: Item1"},
new { Item = 2, Description = "2: Item2"}
};
只需将 BindingSource 添加到 CheckedListBox:
BindingSource bindingSource = new BindingSource(list, null);
checkedListBox1.DataSource = bindingSource;
checkedListBox1.ValueMember = "Item";
checkedListBox1.DisplayMember = "Description";
当您选择了项目并想要查看时:
var checkedItems = checkedListBox1.CheckedItems;
foreach (dynamic checkedItem in checkedItems)
{
Console.WriteLine("valuemember: " + checkedItem.Item);
// or whatever code you have
}
希望对您有所帮助!