Sorting a DataTable

January 13, 2011

Just a quick tip this one. To sort a DataTable…

  1. Clone the Unsorted DataTable
  2. Generate a DataRow array, from the unsorted DataTable using .Select, specifying the search criteria (The first argument is the select criteria, which I have left Null so we get all rows)
  3. Loop the DataRow array, importing the rows into the new table

 

DataTable dtSorted = dt.Clone();
DataRow[] dataRowsSorted = dtUnSorted.Select(null, "PercentSurveysCompleted ASC");
foreach (DataRow dataRow in dataRowsSorted)
{
    dtSorted.ImportRow(dataRow);
}
Advertisements

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

%d bloggers like this: