PDA

Click to See Complete Forum and Search --> : Sorting a datatable for REALS!


silentthread
Nov 28th, 2007, 12:12 PM
Sorting a datatable for REALS! Just like they said in the South Park show where Kenny goes to heaven with his playstation system. "This is for reals."


Okay, folks here it is...

I'm a fan of Response.Write, and the other day, I needed to response.write the contents of a datatable. Don't ask me why. Just know that I had alot of neat javascript and ajax goodies going on. So, it was easier for me to just response.write the whole thing. Then I found out that...

mytable.DefaultView.Sort = "column asc"

Only works when you bind it, example a datagrid. This is a terrible thing. I can't believe, I was mislead to believe that DefaultView.Sort would do the trick. Shame on you world. lol. :-)

Here is a function I created that does real sorting on a datatable....



Public Function SortTable(ByRef mytable As DataTable, ByVal column As String)
'Sorting a table for reals! By SiLentThReaD, AKA Numbchucks.

'create a blank table. tablecopy is the name
Dim tablecopy As New DataTable
'make an exact copy of the table you passed in by reference, and
'store it in your tablecopy table
tablecopy = mytable.Copy()
'clear the contents of the table you just passed in, because
'you will soon fill it with newly sorted rows
mytable.Clear()
'create a rows array
Dim foundRows() As DataRow
'assign the foundrows array the values returned
'from the select. In this case, I left the
'filterexpression blank, because I want to get
'everything. I all I need to change is the sort
'order.
foundRows = tablecopy.Select("", column + " asc")

'iterate through each row and import them into
'the table that you passed in by reference
Dim dbrow As DataRow
For Each dbrow In foundRows
mytable.ImportRow(dbrow)
Next

'finally clear the contents of the tablecopy you created.
tablecopy.Clear()

End Function

jmcilhinney
Nov 29th, 2007, 07:09 AM
This code is, I'm afraid, a waste of time. You don't need to sort the DataTable itself. The reason that sorting the DefaultView works when binding is that the data is taken from the DefaultView by the bound control. If you want to see the data sorted then you just need to get the data from the DefaultView too. Try this:Dim table As New DataTable

table.Columns.Add("Value", GetType(String))

table.Rows.Add("C")
table.Rows.Add("B")
table.Rows.Add("D")
table.Rows.Add("A")

For Each row As DataRow In table.Rows
MessageBox.Show(CStr(row("Value")), "Rows Before Sort")
Next row

For Each row As DataRowView In table.DefaultView
MessageBox.Show(CStr(row("Value")), "DefaultView Before Sort")
Next row

table.DefaultView.Sort = "Value ASC"

For Each row As DataRow In table.Rows
MessageBox.Show(CStr(row("Value")), "Rows After Sort")
Next row

For Each row As DataRowView In table.DefaultView
MessageBox.Show(CStr(row("Value")), "DefaultView After Sort")
Next rowI wrote this in a WinForms project so you can use something other than MessageBox.Show if you like, but it shows that after setting the DefaultView.Sort property the Rows property still returns the data in the original order but the DefaultView returns it in sorted order. You just access the DataRowView objects from the DefaultView instead of the DataRow objects from the Rows collection. If you really need a DataRow for some reason then you can get it from the Row property of the corresponding DataRowView.

silentthread
Nov 29th, 2007, 10:37 AM
Dude, you are an animal. Thanks for sharing. I have given you reputation on this. Thanks again!