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

8

本文介绍了列表框选择的项目给我"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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14