• Home
  • About Me
  • Musings
  • Programming
  • Open Source
  • Computers
  • Society
  • Books
  • Design
  • Movies
  • C#: Sorting a DataTable

    Posted on April 6th, 2009 by Rajeshwaran S P

    Sorting the result in a DataTable’s select command can be done as below.

    1
    2
    
    DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset.
    DataRow[] row = table.Select("id = 500", "name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc.

    The first argument to the select is the filter condition, the second argument is the sort option.

    Trying to use,

    1
    2
    
    DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset.
    DataRow[] row = table.Select("id = 500 order by name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc.

    will result in a error “Missing operand before order”.

    Possibly Related posts:

    1. C#: Null coalescing operator C# has a ?? operator, which is called the ‘Null...
    2. Converting Rows to Columns in SQL There was this table, ORG_ID LEGACY_ORG_CODE ------ ------------------ 1001 8909...
    3. C#: Calculate Age in Years, Month and Days Today I was given the task of finding the age...
    4. Implicit Variable in C# 2008 C# 2008 allows for creating implicit variables using the ‘var’...
    5. Rake Migrate – Aborted! Trying out a sample application using rails, I faced a...

    Tags: ,

    Leave a Reply