SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : Microsoft .NET :

Previous DiscussionNext Discussion
 Really dumb question -- Sorting a DataTable

Ok, I'm working on a console app. I've gotten it to do almost everything I want -- except sort a datatable.

I'm doing this in C#. 1.1 Framework.

Let's say I have a datatable that looks like this:

THIS            5
THAT           1
THE OTHER 3

and I want to sort by the second column, so it reads:

THAT            1
THE OTHER  3
THIS             5

I tried doing myDataTable.DefaultDataView.Sort = "column_2 asc";

and that didn't work. I also tried sorting a data view. I tried a few things I found on Google, but no luck there, either (most of the stuff is transitioning to 2.0 framework).

What do I need to do to get this to sort correctly?

Started By Monte on Feb 21, 2007 at 2:47:24 PM

13 Response(s) | Reply

Earlier Replies | Replies 7 to 13 of 13 | Later Replies
Goto Page: 2 1
brian on Feb 22, 2007 at 8:26:44 AM (# 7)

You need to read the data from the DefaultView instead of the table.

change from:

for(x = 0; x < dt.Rows.Count; x++)
    {
        tw.WriteLine(dt.Rows[x][0].ToString() + "\t" + dt.Rows[x][1].ToString() + "\t" + dt.Rows[x][2].ToString());
    }

to either

foreach (DataRow row in dt.DefaultView) {
    tw.WriteLine(row[0].ToString() + "\t" + row[1].ToString() + "\t" + row[2].ToString());
}

or

for(x = 0; x < dt.Rows.Count; x++)
    {
        tw.WriteLine(dt.DefaultView[x][0].ToString() + "\t" + dt.DefaultView[x][1].ToString() + "\t" + dt.DefaultView[x][2].ToString());
    }

This should work. Give that a try anyway and let me know what happens.


Monte on Feb 22, 2007 at 8:51:18 AM (# 8)

This is the solution I used:

for(x = 0; x < dt.Rows.Count; x++)
    {
        tw.WriteLine(dt.DefaultView[x][0].ToString() + "\t" + dt.DefaultView[x][1].ToString() + "\t" + dt.DefaultView[x][2].ToString());
    }

It worked. Thanks a bunch!!


brian on Feb 22, 2007 at 8:59:18 AM (# 9)

cool. glad you got it sorted.


ChrisRickard on Feb 22, 2007 at 1:49:27 PM (# 10)

Pun intended? :D


Monte on Feb 22, 2007 at 2:00:53 PM (# 11)

Actually, now I have a third DataTable that won't sort...

I even tried the

foreach (DataRow row in dt.DefaultView) {
    tw.WriteLine(row[0].ToString() + "\t" + row[1].ToString() + "\t" + row[2].ToString());
}

But that gave me an error (Specified cast is not valid).

Well, the first two worked with the code I mentioned previously. My table structure is:

DataTable dt = new DataTable();
dt.Columns.Add("TEAM_NAME");
dt.Columns.Add("POWER_RANKING");

And the sort looks like this:

dtPower.DefaultView.Sort = "POWER_RANKING DESC";

But it never sorts correctly. I'm missing something, but I don't know what...


Monte on Feb 22, 2007 at 2:30:41 PM (# 12)

Never mind...I forgot to establish a variable type for one of the columns...problem solved.


brian on Feb 23, 2007 at 12:48:51 AM (# 13)

'fraid not Chris...never thought about it until you pointed it out :)


Earlier Replies | Replies 7 to 13 of 13 | Later Replies
Goto Page: 2 1

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2004 InsideDHTML.com, LLC. All rights reserved.