Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 988
Xaisoft Avatar asked Dec 22 '08 20:12

Xaisoft


People also ask

Which of the following is a DataView method?

Following are the methods of a DataView: Find : Parameter: An array of values; Value Returned: Index of the row. FindRow : Parameter: An array of values; Value Returned: Collection of DataRow. AddNew : Adds a new row to the DataView object.

How do you use data view?

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.

How do I access DataView rows?

You can access the DataRow that is exposed by the DataRowView through the Row property of the DataRowView. When you view values by using a DataRowView, the RowStateFilter property determines which row version of the underlying DataRow is exposed.

How copy data from DataView to DataTable in C#?

DataView. Table. Copy() will copy the source DataTable, not the filtered data of the DataView.


1 Answers

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"];
like image 141
Marc Gravell Avatar answered Oct 23 '22 11:10

Marc Gravell