Question Sorting a dataset with updated data.

ChrisMayhew

Member
Joined
Apr 2, 2009
Messages
12
Programming Experience
1-3
Hello

I am currently working on a project that I load all the required data at the start of the program out of a database into a Data Set however the content updates while using the program.

So for example I have a number that will be updating on a regular basis while the program is running and I want to be able to re-sort the data in Descending order so that the largest number will appear on the top when the form with that data on it loaded.

Currently I have labels on the page that have the data being displayed (tried the data grid views and I don't like them) but I can't get the dataset to re-sort in descending order unless I save the data set to the database and then re-load the program.

So is there anyway I can sort the data thats already in a dataset.

I found this but it didn't work.
VB.NET:
DS.Tables("tblUsers").Select(False, "DPoints", DataViewRowState.CurrentRows)

Thanks,
Chris
 
You can't sort data in a DataTable. That code isn't going to change anything in the DataTable. What it will do is return an array containing the very same DataRows that are in the DataTable and those rows will be sorted in ascending order by DPoints. The order of the rows in the DataTable won't change though.

What you should do is not get the data from the DataTable directly. Get the data from the DefaultView instead. You can set the DefaultView's Sort property to "DPoints" and the DefaultView will always be sorted in ascending order by DPoints. Again, the data in the DataTable will not be affected.
 
You can't sort data in a DataTable. That code isn't going to change anything in the DataTable. What it will do is return an array containing the very same DataRows that are in the DataTable and those rows will be sorted in ascending order by DPoints. The order of the rows in the DataTable won't change though.

What you should do is not get the data from the DataTable directly. Get the data from the DefaultView instead. You can set the DefaultView's Sort property to "DPoints" and the DefaultView will always be sorted in ascending order by DPoints. Again, the data in the DataTable will not be affected.

Thanks, I'll give it a go in a bit and let you know if it works :)
 
Given that you're on .NET 3.5 you should have followed a tutorial from Microsoft such as those found in the DW2 link in my signature (note the panel in the top right for 3.5 specific pages). You'll doubtless have a form, with a load of bound labels, connected to a BindingSource, and you can just set the sort on the BindingSource.

You can additionally have the database sort the data before it returns it
 
Back
Top