I am seeking code for a solution to an auto reverse sort in a datagrid
for a web application in vb.net. It is a typical practice, on the
datagrid header click the text once to sort DESC, click the header
text again to sort ASC. I am having trouble finding functional code.
Below is the code for the sort, bindgrid, and datapage. It currently
only sorts in one direction. Thank you.
Public Sub BindGrid(ByVal strOrder As String)
Dim strSearch = ViewState("SearchProperty")
Dim CommandText As String
If strSearch = "" Then
CommandText = "SELECT * From Property ORDER BY " & strOrder & ""
Else
CommandText = "SELECT * From Property Where " & strSearch
& " ORDER BY " & strOrder & ""
End If
Dim sqlCommand As New SqlDataAdapter(CommandText, sqlConnection)
Dim ds As New DataSet
sqlCommand.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
sqlConnection.Close()
End Sub
Sub DataGrid_Page(ByVal sender As Object, ByVal e As
DataGridPageChangedEventArgs)
DataGrid1.CurrentPageIndex = e.NewPageIndex
If ViewState("SortExprValue") <> "" Then
BindGrid(ViewState("SortExprValue").ToString())
Else
BindGrid("Location")
End If
End Sub
Sub DataGrid_Sort(ByVal sender As Object, ByVal e As
DataGridSortCommandEventArgs)
DataGrid1.CurrentPageIndex = 0
ViewState("SortExprValue") = e.SortExpression
BindGrid(e.SortExpression)
End Sub |