9

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

DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;

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

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

4 回答 4

7

您需要指定要获取其值的行。我可能会更倾向于 table.Rows[index]["GrossPerPop"].ToString()

于 2008-12-22T20:57:32.107 回答
6

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

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

或没有 LINQ:

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

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

如果你有 aDataView而不是 a DataTable,那么同样适用于 a DataRowView

string val = (string)view[rowIndex]["GrossPerPop"];
于 2008-12-22T21:04:21.407 回答
3

@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.
于 2013-10-31T08:19:17.627 回答
1

对于 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
于 2015-03-21T04:51:41.007 回答