Home > .NET > So easy to sort a DataTable!

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 …

Categories: .NET
  1. http://
    February 23, 2004 at 10:08 pm

    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.

  2. http://
    July 21, 2004 at 11:42 am

    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.

  3. http://
    July 22, 2004 at 1:58 pm

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

  4. http://
    August 3, 2004 at 3:38 pm

    Thanks guys, that is exactly what I was looking for

  5. Troy
    March 29, 2007 at 4:29 pm

    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

  6. Damen
    April 18, 2007 at 3:53 am

    Dude, yall don’t need all of that code!

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

  7. http://
    September 13, 2007 at 2:24 am

    Sweet! …so 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!

  8. http://
    October 23, 2007 at 12:14 pm

    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.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: