473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

sorting in DataTable

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
Nov 18 '05 #1
3 1634
Grey wrote:
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
Nov 18 '05 #2

"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

Nov 18 '05 #3
i think its aspalliance ... i think it was a 3 function snippet.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Shock" <no@way.com> wrote in message
news:10*************@corp.supernews.com...
Grey wrote:
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

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
1
by: Jeremy | last post by:
I want my gird to sort only the items on the current page when I click on a column header. I wrote a little test app, but when I sort it pulls in items from other pages and places them on the current...
4
by: Richard | last post by:
When i try sorting in the database, it sorts the numbers: 0 1 102 2 304 305 4 etc....
3
by: WB | last post by:
Hi, I have a DataTable, which I'd like to sort before using it for other operation. However, I notice that even after I call the .DefaultView.Sort = "username", the view is still not sorted. For...
6
by: David P. Donahue | last post by:
I've been looking around for ways to sort the rows in a DataTable, and everything seems to point to just changing the Sort property on that DataTable's DefaultView property. That's all well and...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
4
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a vb.net 2.0 app that is loading a GridView with a DataSource that is returned from a function. The definitions in the function are: Dim ReportDS As DataSet = New DataSet Dim...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
1
by: castron | last post by:
Hello All, I have a grid view that allows sorting, paging, editing, etc. Under On Load event, if I check: if(!IsPostBack){ DisplayData(); }, the Edit portion works fine. However, the Sorting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.