sorting in DataTable

G

Grey

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
 
S

Shock

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
 
C

Cor Ligthert

"Shock" >
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

You make me curious, what?

Cor
 
H

Hermit Dave

i think its aspalliance ... i think it was a 3 function snippet.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
Shock said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top