C# 从绑定源获取行数据绑定到列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/811506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting row data from bindingsource bound to listbox
提问by Daniel Szalay
I have a listbox like so:
我有一个像这样的列表框:
list.DataSource = bindingSource;
list.DisplayMember = "column_name";
Later I would want to get the selected item's ID
from the DataSet
with bindingSource.Current
. I've done this before with bindingNavigator
and bindingSource
, where Current
returns a DataRowView
, so I can cast it and I'm done:
后来我想ID
从DataSet
with 中获取所选项目bindingSource.Current
。我之前用bindingNavigator
and完成了这个bindingSource
,其中Current
返回 a DataRowView
,所以我可以投射它,我就完成了:
Int32.Parse(((DataRowView)bindingSource.Current)["id"].ToString())
But in this case Current
returns a DataViewManagerListItemTypeDescriptor
object, and I can't cast it.
但在这种情况下Current
返回一个DataViewManagerListItemTypeDescriptor
对象,我不能投射它。
Any thoughts will be appreciated!
任何想法将不胜感激!
Daniel
丹尼尔
采纳答案by lc.
list.SelectedItem
should contain the selected row's DataRowView
. Then you can:
list.SelectedItem
应包含所选行的DataRowView
. 然后你可以:
var row = (MyRowType)((DataRowView)list.SelectedItem).Row;