So easy to sort a DataTable!

Published Monday, September 15, 2003 12:59 AM

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 ...
Filed under:

Comments

# Johnny Hall said on Monday, February 23, 2004 5: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.

# Mose said on Wednesday, July 21, 2004 7: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.

# Oleg said on Thursday, July 22, 2004 9:58 AM

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

# Daniel said on Tuesday, August 3, 2004 11:38 AM

Thanks guys, that is exactly what I was looking for

# Troy said on Thursday, March 29, 2007 12: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

               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

# Damen said on Tuesday, April 17, 2007 11:53 PM

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

           if (ddlSort.SelectedIndex > 0)

           {

               dataRows = dtResult.Select(strSQLExpression,ddlSort.SelectedValue);

           }

           else

           {

               dataRows = dtResult.Select(strSQLExpression);

           }

# Scott H. said on Wednesday, September 12, 2007 10:24 PM

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!

# Chris said on Tuesday, October 23, 2007 8:14 AM

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.

This Blog

Syndication