C# 如何从 DataView 中的列中获取值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/387334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a value from a column in a DataView?
提问by Xaisoft
I have a dataview defined as:
我有一个数据视图定义为:
DataView dvPricing = historicalPricing.GetAuctionData().DefaultView;
This is what I have tried, but it returns the name, not the value in the column:
这是我尝试过的,但它返回名称,而不是列中的值:
dvPricing.ToTable().Columns["GrossPerPop"].ToString();
采纳答案by BFree
You need to specify the row for which you want to get the value. I would probably be more along the lines of table.Rows[index]["GrossPerPop"].ToString()
您需要指定要为其获取值的行。我可能会更倾向于 table.Rows[index]["GrossPerPop"].ToString()
回答by Marc Gravell
You need to use a DataRow
to get a value; values exist in the data, not the column headers. In LINQ, there is an extension method that might help:
您需要使用 aDataRow
来获取值;值存在于数据中,而不是列标题中。在 LINQ 中,有一个扩展方法可能会有所帮助:
string val = table.Rows[rowIndex].Field<string>("GrossPerPop");
or without LINQ:
或没有 LINQ:
string val = (string)table.Rows[rowIndex]["GrossPerPop"];
(assuming the data isa string... if not, use ToString()
)
(假设数据是一个字符串...如果不是,使用ToString()
)
If you have a DataView
rather than a DataTable
, then the same works with a DataRowView
:
如果您有 aDataView
而不是 a DataTable
,那么同样适用于 a DataRowView
:
string val = (string)view[rowIndex]["GrossPerPop"];
回答by Ashay
@Marc Gravell .... Your answer actually has the answer of this question. You can access the data from data view as below
@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.
回答by nghiavt
for anyone in vb.NET:
对于 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