Sorting a DataTable...

  • Thread starter Thread starter Colin Basterfield
  • Start date Start date
C

Colin Basterfield

Hi,

I have a DataSet whose default table, namely DataSet.Tables[0] which has a
row for each day selected from a database, however not all the days are
always there so I am iterating thro each day and if it is missing I add it
to the table by way of a new row with default values. The DataSet is then
passed thro a web service to a Web front end and then shown on a grid.

The problem I have is that I want the days to appear Mon thro' Sunday, but
as I am adding them at the end of the Rows, they will not appear in the
right order. I thought about using InsertAt, but depending on the number of
days already there or inserted it would be tedious to have to work out which
position to InsertAt, to get them in the right order at that stage.

So to save my little brain getting a buzzing noise is there an existing way
to do this, I was thinking of using DataTable.Select, but wasn't sure how to
get it back into the original dataset to pass thro'.

Any ideas?
TIA
Colin B
 
Where it physically gets inserted, If I understand the problem correctly,
really should matter. You can insert things anywhere, and then right before
you submit your data, sort it in a fashion you want and submit it. Since
weekdays aren't alphabetical, you may want to use an Enum and store that
value as a DataColumn. You can add the value to your datatable/dataview and
not submit it to the web service b/c it's not needed for anythign but
sorting.

So, public enum DaysOfWeek : uint{
Monday
, Tuesday
, Wednesday //etc)

}

Then create a DataColumn and add these values to each day you know you have.
When you add a record that wasn't in there, just add the enum value to the
record as well. Assuming that Column was called DayOfWeekOrdinal.... then
you could simply use the dataview and use (assuming you hvae a DataTable dt)

DataView dv = dt.DefaultView();
dv.Sort = "DayOfWeekOrdinal";
http://www.knowdotnet.com/articles/dataviewsort.html

HTH,

Bill
 
Back
Top