eugeneK
eugeneK

Reputation: 11136

HowTo reorder items in DataView object

I have a DataView object with productID and productName, i get all items from Products Table and store them in Cache. I want to get items that user bought in order he bought them without using another query or joining tables.

ie. Products DataView 1. Foo 2. Foo2 3. Foo3

Filtering to ProductsBoughtByUser using RowFilter (productID in (1,3)) UserProducts DataView 1.Foo 3.Foo3

Now user first bought Foo3, how do i reorder items based on correct ( chronological ) Array. ie 3 1

Thanks

Upvotes: 0

Views: 513

Answers (1)

Jim Schubert
Jim Schubert

Reputation: 20357

Here is the syntax for DataView.Sort:

private void SortByTwoColumns()
{
    // Get the DefaultViewManager of a DataTable.
    DataView view = DataTable1.DefaultView;

    // By default, the first column sorted ascending.
    view.Sort = "State, ZipCode DESC";
}

From your original post, it sounds like you only have Product ID and Product Name. You can't sort these in memory unless you're also retrieving a purchase/order date.

Probably the easiest thing to do would be to add the date column (PurchaseDate) to your sql statement that you're already using, then do a view.Sort = "PurchaseDate DESC";

That way, you don't have to write another statement or hit the database a second time for this sort.

Edit: The DataRowView has integral and string indexers. e.g.:

foreach (DataRowView row in dataView)
{ 
   var value = row["COLUMN_NAME"]; 
}

The column is stored as an object, so you'll have to check for null and convert to your data type

Upvotes: 5

Related Questions