Grey said:
I have create a DataTable in DataSet and I want to have different sorting in
this datatable from time to time. Is it possible to sort the data within the
datatable, so i can sort the datagrid easily.
Million thanks
Here is a little method I used to sort a datatable. Not quite sure
where I grabbed the code, but I put it in a utility library I use
regularly at work.
/// <summary>
/// FilterSortData is used to sort a datatable directly.
/// </summary>
/// <param name="dtStart">The DataTable to be sorted.</param>
/// <param name="filter">A way to filter out certain rows from the
table.</param>
/// <param name="sort">A way to sort the table (i.e. "State IN
'CA'")</param>
/// <returns>A Sorted/Filtered DataTable</returns>
public static DataTable FilterSortData(DataTable dtStart, string filter,
string sort)
{
DataTable dt = dtStart.Clone();
DataRow[] drs = dtStart.Select(filter, sort);
foreach (DataRow dr in drs)
{
dt.ImportRow(dr);
}
return dt;
}
There could be an easier way, but the only one I can think of would be
to use a dataview, which would be great except you would lose some of
the functionality of the table by switching to a view via something like
the following:
DataView dv = new DataView(theDataTable);
dv.sort = "columnname";
Hope I helped,
Shock