How to get a value from a column in a DataView?

谁说胖子不能爱 提交于 2019-11-30 19:04:24

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()

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:

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

or without LINQ:

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

(assuming the data is a string... if not, use ToString())

If you have a DataView rather than a DataTable, then the same works with a DataRowView:

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

@Marc Gravell .... Your answer actually has the answer of this question. You can access the data from data view as below

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.
nghiavt

for anyone in 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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!