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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:08:15  来源:igfitidea点击:

getting row data from bindingsource bound to listbox

c#.netdata-bindinglistbox

提问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 IDfrom the DataSetwith bindingSource.Current. I've done this before with bindingNavigatorand bindingSource, where Currentreturns a DataRowView, so I can cast it and I'm done:

后来我想IDDataSetwith 中获取所选项目bindingSource.Current。我之前用bindingNavigatorand完成了这个bindingSource,其中Current返回 a DataRowView,所以我可以投射它,我就完成了:

Int32.Parse(((DataRowView)bindingSource.Current)["id"].ToString())

But in this case Currentreturns a DataViewManagerListItemTypeDescriptorobject, and I can't cast it.

但在这种情况下Current返回一个DataViewManagerListItemTypeDescriptor对象,我不能投射它。

Any thoughts will be appreciated!

任何想法将不胜感激!

Daniel

丹尼尔

采纳答案by lc.

list.SelectedItemshould contain the selected row's DataRowView. Then you can:

list.SelectedItem应包含所选行的DataRowView. 然后你可以:

var row = (MyRowType)((DataRowView)list.SelectedItem).Row;