Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataSet sorting?

Status
Not open for further replies.

TCavins

Programmer
Aug 8, 2001
25
US
I have a dataset as

DataSet oDS;

and I'm populating it with data so that I can read it as

foreach (DataRow oDR in oDS.Tables[0].Rows)
{
...
}

However, I would like to be able to sort the DataSet before reading it all in the foreach statement.

I've tried oDS.Tables[0].Select("","BulletinNumber DESC");
and that hasn't done anything.

Anyone know how I can sort my DataSet on one of its columns?

thanks in advance,
Tim
 
Okay, I'm trying something else along with what I have above.

I've set up a DataView as shown below...

DataTable dt = new DataTable();
DataView dv = oDS.Tables[0].DefaultView;
dv.Sort = "BulletinDate DESC";


I'm now looping through the dataview as:

foreach (DataRow oDR2 in dv.Table.Rows)
{
Response.Write(oDR2[&quot;BulletinDate&quot;] + &quot;<BR>&quot;);
}

Now this outputs fine, however, it isn't sorting like I thought it would. Am I missing something?

-Tim
 
Nope, that's how you do it... If you apply the sort directly against your database, do you get the same results? If so, then it's a data problem.

As an aside... you should try to steer completely clear of response.write() statements. There's nothing that you can't do without them w/ new ASP.NET controls, be it manually creating a table and adding to the controls collection of a placeholder on your page, or using databound controls (datagrid, datalist, repeater, etc...) to display data.

For instance, with your statement there, a simple datagrid bound to your datasource would work very nicely. Just some advice on changing design patterns that will make your life (or at least your job) much simpler. :)

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Paul,

Thanks for responding.

When I add the OrderBy in the SQL statement it works fine, just can't get it to work in .net for the dataview. Do you have any other ideas?

The Response.Write is only for debugging purposes. Once I get the sort to work, I am going to remove it.

Thanks,
-Tim
 
Tim,

you set the sort property of the DataView, not the DataTable (the DataTable has no Sort property), so you have to loop through the DataRowViews of the DataView instead of the DataRows of the DataTable to get the sorted rows.
Your code gets the rows from dv.Table.Rows and that's nothing else than a reference to dt.Rows.
Try the following:

DataTable dt = new DataTable();
DataView dv = oDS.Tables[0].DefaultView;
dv.Sort = &quot;BulletinDate DESC&quot;;
foreach (DataRowView oDR2 in dv)
{
Response.Write(oDR2[&quot;BulletinDate&quot;] + &quot;<BR>&quot;);
}

Hope this helps,
Tobias
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top