Sort a datatable

Hi

I have a datatable that i would like to sort by a column (not the key column).

How should one sort a table

Can the sorted table have the same name as se unsorted table

I don't know now the the name of the current column (the column that should be the index for the sorted table, only the number of order ex. aRow[120]), will that be a problem

Br
Matt



Answer this question

Sort a datatable

  • Chin Tian

    You can try using the Select method of the DataTable class. This allows for you to sort also. To get the associated column name to sort by using the index of the column, you can use the existing DataColumnCollection exposed by the Columns property. After you call the Select method you can import the sorted array of DataRows into a new DataTable via the ImportRow method.

    Depending on how the original DataTable was created you can have the same name.

    smc750



  • senordotnet

    This is how I accomplished the task, however I do not this the class realy needs to be used in this instance I have future use for it

    Using the click event I take the values from a listbox and sort them

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

    Dim lcol As New ListItemCollection
    Dim utility As New clsUtilities
    Dim dt As New DataTable
    Dim lstItm As ListItem

    For Each lstItm In lbxUsers.Items

    If lstItm.Selected Then
    lcol.Add(lstItm)
    End If

    Next
    Dim intCounter As Int16

    dt = utility.ResetListBox(lcol)
    Dim strColumnName As String = dt.Columns(0).ColumnName
    Dim myDataView As New DataView(dt)

    myDataView.Sort = strColumnName
    For intCounter = 0 To dt.Rows.Count - 1
    lbxAcceptedUsers.Items.Add(myDataView(intCounter).Item(1))
    Next

    For intCounter = 0 To lcol.Count - 1

    lbxUsers.Items.Remove(lcol(intCounter))
    lbxAcceptedUsers.ClearSelection()

    Next

    End Sub

    this class noted above "dt = utility.ResetListBox(lcol)" I create a datatable from listbox1's values and use them
    to load the second listbox2

    Public Function ResetListBox(ByVal lcolRaw As ListItemCollection) As DataTable

    'Dim x As Int16
    Dim dt As New DataTable
    Dim dr As DataRow
    Dim itm As ListItem = Nothing


    With dt.Columns

    .Add("text", System.Type.GetType("System.String"))
    .Add("value", System.Type.GetType("System.String"))

    End With

    For Each itm In lcolRaw
    If itm.Text <> "" Then

    dr = dt.NewRow

    With dr

    .Item("text") = itm.Text
    .Item("value") = itm.Value

    End With

    dt.Rows.Add(dr)

    End If
    Next


    dt.AcceptChanges()


    Return dt
    End Function


  • Mike Fang

    I wrote a couple of tutorials that talk about sorting a DataTable. I recommend creating a DataView from the DataTable:

    DataView Sorting Filtering and DataBinding in ADO.NET 2.0 - Converting DataView to Table - ADO.NET Tutorials

    Regards,

    Dave



  • Douglas Chor

    Ideally if you only need to sort the data once then you should sort the data on the DB side when it is returned. However assuming that you support client-side sorting (such as column headers) then you can use the DataTable.Select() method to retrieve the desired rows and sort them at the same time. The first parameter is the filtering criteria while the second parameter is the column name(s) to sort on and the optional direction (such as "Name ASC"). The returned collection will be filtered and sorted appropriately. Note that I believe the filter can be empty to indicate all rows. Look at DataColumn.Expression for how to build this string. To get the column name from a column index use DataTable.Columns[index].ColumnName.

    Michael Taylor - 5/15/06


  • Sort a datatable