C#: Sorting a DataTable

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

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,

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”.

Comments

2 responses to “C#: Sorting a DataTable”

  1. kaviya Avatar

    how to calculate age from date of birth.i have only one date time picker current date should be subtracted with the chosen (b’day) date in date time picker. the age will display in text box how to work this.

    1. Rajeshwaran S P Avatar

      @Kaviya: On the DayPicked() Event of the date picker control, call the method discussed in the post to calculate age. Then populate the value got from the calculation on the Age Text Box.

Leave a comment