By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

sorting a DataTable

P: n/a
Bob
I need to sort the contents of a DataTable by a column *after* it's been
populated. Is this possible?

Bob

Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Bob" <no****@nowhere.com> wrote in news:#Nk4Z4PaEHA.2844
@TK2MSFTNGP12.phx.gbl:
I need to sort the contents of a DataTable by a column *after* it's been
populated. Is this possible?

You can apply a Dataview to the DataTable. Or you can issue a
datatable.select and order it via that way as well.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #2

P: n/a
Hi Bob,

dim dv as new dataview(myDatatable)
dv.sort = "mysortcolumn"

You can access the dv a little bit the same as the datatable.

I hope this helps,

Cor
Nov 20 '05 #3

P: n/a
Actually, tables in a have a default view. This is normally presented for
example to a DataGrid's DataSource.

Ds1.Tables(0).DefaultView.Sort = "LastName"

Regards - OHM
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"Bob" <no****@nowhere.com> wrote in news:#Nk4Z4PaEHA.2844
@TK2MSFTNGP12.phx.gbl:
I need to sort the contents of a DataTable by a column *after* it's been
populated. Is this possible?

You can apply a Dataview to the DataTable. Or you can issue a
datatable.select and order it via that way as well.
--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 20 '05 #4

P: n/a
Bob
I need the row indexes in a particular order, TBD after population. Will I
just have to create a new DataTable?

Bob

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
You can apply a Dataview to the DataTable. Or you can issue a
datatable.select and order it via that way as well.


Nov 20 '05 #5

P: n/a
Bob

Private Sub SortTable(ByVal dt As DataTable, ByVal col As DataColumn)
Dim SortValues(dt.Rows.Count - 1) As Object
Dim SortIndex(dt.Rows.Count - 1) As Integer
For i As Integer = 0 To dt.Rows.Count - 1
SortIndex(i) = i
SortValues(i) = dt.Rows(i)(col)
Next
System.Array.Sort(SortValues, SortIndex)
For i As Integer = 0 To SortIndex.GetUpperBound(0)
dt.ImportRow(dt.Rows(SortIndex(i)))
Next
For i As Integer = 0 To SortIndex.GetUpperBound(0)
dt.Rows.RemoveAt(0)
Next
End Sub
Nov 20 '05 #6

P: n/a
Hi Bob,

Is this not more simple?
\\\
Dim dv As New DataView(dt)
dv.Sort = "bla"
Dim dtnew As DataTable = dt.Clone
For Each dvr As DataRowView In dv
dtnew.ImportRow(dvr.Row)
Next
///

Cor
Nov 20 '05 #7

P: n/a
Bob,
In addition to Cor's suggestion of using the DataView to sort.

You can use DataTable.Clear to remove all the rows.

dt.Clear()

Hope this helps
Jay

"Bob" <no****@nowhere.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...

Private Sub SortTable(ByVal dt As DataTable, ByVal col As DataColumn)
Dim SortValues(dt.Rows.Count - 1) As Object
Dim SortIndex(dt.Rows.Count - 1) As Integer
For i As Integer = 0 To dt.Rows.Count - 1
SortIndex(i) = i
SortValues(i) = dt.Rows(i)(col)
Next
System.Array.Sort(SortValues, SortIndex)
For i As Integer = 0 To SortIndex.GetUpperBound(0)
dt.ImportRow(dt.Rows(SortIndex(i)))
Next
For i As Integer = 0 To SortIndex.GetUpperBound(0)
dt.Rows.RemoveAt(0)
Next
End Sub

Nov 20 '05 #8

P: n/a
Bob
Yes, certainly. But I ran into the problem of already having variables
pointing to columns in the original table so I couldn't use a newly created
one. I would definitely use your solution otherwise.

Bob

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:eK**************@TK2MSFTNGP11.phx.gbl...
Hi Bob,

Is this not more simple?
\\\
Dim dv As New DataView(dt)
dv.Sort = "bla"
Dim dtnew As DataTable = dt.Clone
For Each dvr As DataRowView In dv
dtnew.ImportRow(dvr.Row)
Next
///

Cor


Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.