So easy to sort a DataTable!

Up until now my applications usually return arrays of "business objects" from the middle tier and most of my data UI is geared around handling these arrays. I have a customized DataGrid which handles the paging and sorting of these arrays by default. But tonight, I had a need to sort a simple DataTable since it just wasn't worth creating thousands of "business objects" to store two attributes ... I was impressed at how simple it was to convert my DataGrid to support sorting of the DataTable.

Here is a snippet from my customized control:

if (this.DataSource is DataTable) 
	DataTable table = (DataTable) this.DataSource;
	if (sortExpression != null && sortExpression.Length > 0) 
		table.DefaultView.Sort = sortExpression + " " + sortOrder;
The sortExpression variable is obtained from the DataGrid and persisted in ViewState. Maybe I should package up my DataGrid so others can try it ...


  • I'd certainly be interested in your datagrid, I'm always interested in studying others server controls.

    I'm using "business objects" and custom collections also.

  • In fact, you don't sort the DataTable, but only the way it is displayed.

    To sort the DataTable it self, you'll need to use the Select method of the DataTable class.

  • If you want to sort by a primary key, just use DataTable.Select() with no parameters.

    No need for a DataView.

  • Thanks guys, that is exactly what I was looking for

  • This is how I accomplished the task

    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
    End If

    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

    For intCounter = 0 To lcol.Count - 1



    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


    End If


    Return dt
    End Function

  • Dude, yall don't need all of that code!

    if (ddlSort.SelectedIndex > 0)
    dataRows = dtResult.Select(strSQLExpression,ddlSort.SelectedValue);
    dataRows = dtResult.Select(strSQLExpression);

  • Sweet! nice to have mini-solutions like this to avoid sifting through page after page of an ADO tome to find a simple answer to something like this. Thanks!

  • Thanks! I always appreciate it when I find help on the Net, and especially in a place where there's no gimmick like having to create a profile and login etc.

Comments have been disabled for this content.