繁体   English   中英

如何从DataView中的列获取值?

[英]How to get a value from a column in a DataView?

我有一个数据视图定义为:

DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;

这是我尝试过的,但它返回的是名称,而不是列中的值:

dvPricing.ToTable().Columns["GrossPerPop"].ToString();

您需要指定要获取值的行。 我可能更喜欢table.Rows [index] [“GrossPerPop”]。ToString()

您需要使用DataRow来获取值; 值存在于数据中,而不是列标题中。 在LINQ中,有一个可能有帮助的扩展方法:

string val = table.Rows[rowIndex].Field<string>("GrossPerPop");

或没有LINQ:

string val = (string)table.Rows[rowIndex]["GrossPerPop"];

(假设数据一个字符串......如果不是,请使用ToString()

如果你有一个DataView而不是DataTable ,那么同样适用于DataRowView

string val = (string)view[rowIndex]["GrossPerPop"];

@Marc Gravell ....你的答案实际上有这个问题的答案。 您可以从数据视图访问数据,如下所示

string val = (string)DataView[RowIndex][column index or column name in double quotes] ;
// or 
string val = DataView[RowIndex][column index or column name in double quotes].toString(); 
// (I didn't want to opt for boxing / unboxing) Correct me if I have misunderstood.

对于vb.NET中的任何人:

Dim dv As DataView = yourDatatable.DefaultView
dv.RowFilter ="query " 'ex: "parentid = 1 "
for a in dv
  dim str = a("YourColumName") 'for retrive data
next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM