Monday, March 7, 2011

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();
From stackoverflow
  • 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()

    Xaisoft : Ok, Thanks, I was afraid of that.
    configurator : Why afraid? How would you otherwise get the value?
    Xaisoft : I know. Just thinking to self.
  • 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"];
    

0 comments:

Post a Comment