【发布时间】:2008-12-22 20:53:52
【问题描述】:
我有一个数据视图定义为:
DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;
这是我尝试过的,但它返回的是名称,而不是列中的值:
dvPricing.ToTable().Columns["GrossPerPop"].ToString();
【问题讨论】:
我有一个数据视图定义为:
DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;
这是我尝试过的,但它返回的是名称,而不是列中的值:
dvPricing.ToTable().Columns["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
【讨论】:
您需要指定要获取其值的行。我可能更倾向于 table.Rows[index]["GrossPerPop"].ToString()
【讨论】: