2

I need to sort a datatable. I do it with linq this an example

Dim query = From c In dt.AsEnumerable _
              Order By c.Field(Of DateTime?)("LastPurchaseDate"), _
                       c.Field(Of String)("LastName") Descending
            Dim dv As DataView = query.AsDataView

But my problem is that i need to sort the fields not by passing the name of the column but the index of that. Is there a way?

2
  • well I have trouble with the name, because I am using Greek names and it sort of blowing the code up. When i use english it works fine Commented Mar 23, 2012 at 15:28
  • I am doing this also, Dim query = From c In dt.AsEnumerable _ Order By c.Field(Of String)(10) Ascending Dim dv As DataView = query.AsDataView but it doesn't work it keeps the same sort, when I gather the dv.Table Commented Mar 23, 2012 at 15:36

2 Answers 2

5
Dim dt As New DataTable
Dim orderedby = From p As DataRow In dt.Rows Order By p.Item(0) Ascending, 
p.Item(1) Ascending 
Select p cast p as datarow
Sign up to request clarification or add additional context in comments.

Comments

2

Finally I managed to do this using clone method of a datatable.

    Dim DataTableNew As DataTable = New DataTable
    DataTableNew = dt.Clone
    Dim query = (From c In dt.AsEnumerable Order By c.Field(Of String)(12) Ascending)
    query.CopyToDataTable(DataTableNew, LoadOption.OverwriteChanges)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.