如何从DataView中的列获取值?
我有一个数据视图定义为:
DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;
这是我尝试过的,但它返回的是名称,而不是列中的值:
dvPricing.ToTable().Columns["GrossPerPop"].ToString();
您需要指定要获取值的行。 我可能更喜欢table.Rows [index] [“GrossPerPop”]。ToString()
您需要使用DataRow
来获取值; 值存在于数据中,而不是列标题中。 在LINQ中,有一个可能有帮助的扩展方法:
string val = table.Rows[rowIndex].Field("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