11

I have Dataset that include table Items, How can I sort this table by Code field ?

thank's in advance

Gold
  • 60,526
  • 100
  • 215
  • 315

3 Answers3

23

With DataTable, you usually sort a DataView - for example:

        DataTable table = dataSet.Tables["foo"];
        DataView view = table.DefaultView;
        view.Sort = "Code";

then work with view.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3
DataSet fileTransferDetail = null; //Data to be sorted.
DataSet result = null; //Declare a dataSet to be filled.

// Sort data
fileTransferDetail.Tables[0].DefaultView.Sort = "ID DESC";
// Store in new Dataset
result.Tables.Add(fileTransferDetail.Tables[0].DefaultView.ToTable());
Helen
  • 87,344
  • 17
  • 243
  • 314
Taran
  • 2,895
  • 25
  • 22
1

Use a DataView object and call the Sort method on it. Or, if your dataset came from SQL, use the ORDER BY clause to sort it before it gets loaded into the dataset.

Jesse Weigert
  • 4,714
  • 5
  • 28
  • 37
  • The `DataView` object has no `Sort` method. It *does* have a `Sort` *property*; although that doesn't explain how to sort the `DataView`. – Ian Boyd Jun 20 '12 at 19:36