从 DataView C# 中获取价值

发布于 2024-08-04 18:50:15 字数 40 浏览 5 评论 0原文

如何从 DataView.CurrentItem 查找某些列的值。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-08-11 18:50:15

正如 Paul 在评论中指出的那样,DataView 类中没有 CurrentItem 成员。

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

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

同样,如果您有一个 DataRowView 实例(DataRow 的视图) >),你可以这样做:

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

编辑:我刚刚注意到你问题上的 WPF 标签...也许你正在谈论 CollectionView,而不是 DataView

CollectionView 本身没有“列”,但它可以在 GridViewDataGrid(两者都有列)中表示。它只是对象集合的视图。要访问当前对象的特定字段或属性,有两个主要选项:

  • 如果您静态地知道集合项的实际类型:将 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;

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

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

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

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 :

  • 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文