C# 从绑定到listbox的bindingsource获取行数据
我有这样一个列表框:C# 从绑定到listbox的bindingsource获取行数据,c#,.net,data-binding,listbox,C#,.net,Data Binding,Listbox,我有这样一个列表框: list.DataSource = bindingSource; list.DisplayMember = "column_name"; 稍后,我想使用bindingSource.Current从DataSet获取所选项目的ID。我以前用bindingNavigator和bindingSource做过这件事,其中Current返回一个DataRowView,因此我可以强制转换它,我完成了: Int32.Parse(((DataRowView)bindingSource.C
list.DataSource = bindingSource;
list.DisplayMember = "column_name";
稍后,我想使用bindingSource.Current
从DataSet
获取所选项目的ID
。我以前用bindingNavigator
和bindingSource
做过这件事,其中Current
返回一个DataRowView
,因此我可以强制转换它,我完成了:
Int32.Parse(((DataRowView)bindingSource.Current)["id"].ToString())
但是在本例中,Current
返回一个DataViewManagerListItemTypeDescriptor
对象,我不能强制转换它
如果您有任何想法,我们将不胜感激
Daniel
列表。SelectedItem
应包含所选行的DataRowView
。然后你可以:
var row = (MyRowType)((DataRowView)list.SelectedItem).Row;
我尝试了:((DataRowView)szakList.SelectedItem).Row[“id”],但仍然收到无法从DataViewManagerListItemTypeDescriptor强制转换的错误。我做错什么了吗?:\没问题。很高兴我能帮忙。