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

Advertisements

2 thoughts on “C#: Sorting a DataTable

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s