列表框选择的项目给我"System.Data.DataRowView", C# Winforms

listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)

本文介绍了列表框选择的项目给我"System.Data.DataRowView", C# Winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 listbox1 - 它的数据源是一列(产品名称).

I have listbox1 - its datasource is a column (productname).

所以我在列表框中有一个 MultiSelection 选项.

so i have in the listbox a MultiSelection option.

我正在尝试为我选择的所有选项制作一个 MessageBox 代码:

and im trying to make a MessageBox for all the option i selected and this the code:

  foreach (object selectedItem in listBox1.SelectedItems)
  {
       MessageBox.Show((selectedItem.ToString() + Environment.NewLine));
   }

问题是我得到了这个值System.Data.DataRowView

推荐答案

如何填充列表框(即数据源到底是什么)?

How do you populate the listbox (ie what is exactly the datasource)?

根据您的评论,我会说一个 DataView(其中包含 DataRowView...)

From your comment I would say a DataView (and wich contains DataRowView...)

因此,您只需要将 SelectedItem 转换为 DataRowView 即可从此 DataRowView 中获取值:

So you just need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView:

foreach (object selectedItem in listBox1.SelectedItems)
{
     DataRowView dr = (DataRowView)selectedItem;
     String result = dr["productname"].ToString;
     MessageBox.Show(result + Environment.NewLine);
}

可能会关注这篇文章的 VB.Net 开发人员也可能对这个感兴趣.

The VB.Net developers that could fall on this post could also be interested in this.

这篇关于列表框选择的项目给我"System.Data.DataRowView", C# Winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:列表框选择的项目给我"System.Data.DataRowView", C# Winforms

基础教程推荐