How do you accomplish multiple column sorting in a datagrid

jamp01

New member
Joined
Jun 30, 2005
Messages
1
Location
chicago
Programming Experience
Beginner
I'd like to sort each column in a datagrid. What needs to be done using the following code. Sorting works for the one column but I'd like it on all columns.
My thought is that I need a way to change the sortCriteria for each field. I have sortCriteria listed for each column on the HTML page. Any suggestions would be greatly appreciated. Thanks.

This code is in the page_load event
Me.sortCriteria = "MediaType"

Me.sortDir = "desc"

'POPULATE THE MASTER DATAGRID

dgrItems.DataBind()

--------------------------------------------
'HOLDS THE COLUMN NAME TO BE SORTED
Public Property sortCriteria() As String

Get

Return CStr(viewstate("sortCriteria"))

End Get

Set(ByVal Value As String)

ViewState("sortCriteria") = Value

End Set

End Property

------------------------------------------------
'HOLDS THE DIRECTION TO BE SORTED
Public Property sortDir() As String

Get

Return CStr(viewstate("sortDir"))

End Get

Set(ByVal Value As String)

ViewState("sortDir") = Value

End Set

End Property
-------------------------------------------------------------------
Sub dgrItems_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgrItems.SortCommand

'CHECK TO SEE IF THE COLUMN WE CLICKED ON IS THE CURRENT COLUMN DEFINED

'IN THE SORTCRITERIA PROPERTY

If Me.sortCriteria = e.SortExpression Then

If Me.sortDir = "desc" Then

Me.sortDir = "asc"

Else

Me.sortDir = "desc"

End If

End If

'ASSIGN THE COLUMN CLICKED TO THE SORTCRITERIA PROPERTY

Me.sortCriteria = e.SortExpression

'CALL THE PROCEDURE TO RE-DISPLAY THE DATA GRID

Dim myConnection As SqlConnection

Dim myCommand As SqlDataAdapter

Dim cid = ddlMalls.SelectedValue

Dim mallid = ddlMalls.SelectedItem.Value

myConnection =
New SqlConnection("workstation id=3S19H02;packet size=4096;user id=media;data source=""NETAPPS-DEV"";" & _

"persist security info=False;initial catalog=media")

myCommand =
New SqlDataAdapter("SELECT ItemID,ItemCode,MallID,Comments,Dimensions,Number,Description,Restrictions,DimFT2,DimInches2,DimFT3,DimInches3,DimFT4,DimInches4,DimFT5,DimInches5,DimInches,DimFT,type.typedescription as mediatype,Partner,MarketValue FROM dbo.Item, dbo.type WHERE MallID ='" & mallid & "' and type.typecode = item.mediatype ORDER BY '" & Me.sortCriteria & "'" & " " & Me.sortDir, myConnection)

Dim ds As DataSet = New DataSet

myCommand.Fill(ds, "Item")

dgrItems.DataSource = ds

dgrItems.DataBind()

End Sub

 
Back
Top