从数据视图C#中获得价值 [英] Getting Value from DataView C#

查看:174
本文介绍了从数据视图C#中获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从DataView.CurrentItem找到一些列的值。

How to find a value of some column from DataView.CurrentItem.

推荐答案

由于保罗指出,在他的评论,有一个在 CurrentItem 成员>数据视图类。

As Paul pointed out in his comment, there is no CurrentItem member in the DataView class.

如果您知道该项目的索引,你可以通过它的名称,如下所示访问列:

If you know the index of the item, you can access a column by its name as shown below :

string name = dataView[index]["Name"] as string;

同样,如果您有实例的 DataRowView的,你可以做(​​的的DataRow 视图)即:

Similarly, if you have an instance of a DataRowView (a view of a DataRow), you can do that :

string name = dataRowView["Name"] as string;



编辑:我只注意到了WPF标记上你的问题......也许你是在谈论一个的CollectionView ,不是数据视图

I just noticed the WPF tag on your question... perhaps you're talking about a CollectionView, not DataView ?

的CollectionView 没有列本身,而是它可以重新psented在的GridView 的DataGrid (两者都列)。这只是一个欣赏对象的集合。访问当前对象的特定字段或属性,有两个主要的选项:

CollectionView doesn't have "columns" per se, but it can be represented in a GridView or DataGrid (which both have columns). It's just a view over a collection of objects. To access a specific field or property of the current object, there are two main options :

  • 如果您静态地知道藏品的实际类型:投 CurrentItem 该类型,并直接访问您需要的成员
  • 如果您不知道类型,你可以使用反射在CurrentItem通过名称来访问它的属性或字段
  • if you statically know the actual type of the collection items : cast the CurrentItem to that type, and directly access the members you need
  • if you don't know the type, you can use reflection on the CurrentItem to access its properties or fields by name

这篇关于从数据视图C#中获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆