Sort gridview columns ascending and descending

Dan181920

Active member
Joined
Jun 28, 2011
Messages
27
Programming Experience
Beginner
Hello,

I am trying to program my page so that when a user clicks on a column heading of a gridview, the data is sorted either in ascending or descending order.

So far, I have done it so that the gridview can be sorted:

ASP.NET Syntax
  1. <asp:GridView ID="GridView1" runat="server" Height="143px" AllowSorting="true"



I get a message saying:

The GridView 'GridView1' fired event Sorting which wasn't handled.

I dont quite understand what this means.

Can anybody advise please on what this means and what I need to do to overcome this?

Many thanks in advance.

Dan
 
Hi Menthos,

I have added the following code for the handler, but I am still not getting anything. Would you be able to advise at all?

Protected Sub Button3_Click(ByVal sender AsObject,ByVal e AsEventArgs)Handles Button3.Click
Dim sqlConn AsNewSqlConnection
Dim sqlCmd AsNew SqlClient.SqlCommand
Dim sqlReader AsSqlDataReader
'If no values are supplied in the textbox, throw an error message.
If TextBox2.Text = ""Then
MsgBox("A centre code needs to be provided...")
EndIf
If TextBox2.Text <> ""Then
'Telling the system the location of the database.
sqlConn.ConnectionString = "server=servername;Initial Catalog=dbName;Trusted_Connection=yes"
'Here we are opening the connection to the database.
sqlConn.Open()
'This is to say that sqlCmd is a stored procedure.
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
'This is creating the command to execute the stored procedure based on the information given in the connection string.
sqlCmd = sqlConn.CreateCommand
'The command is triggered to execute the stored procedure which grabs all information for the specific centre.
sqlCmd.CommandText = "exec GetAllInformationKCSEBoxes '" & TextBox2.Text & "' "
'This will read the rows in the database.
sqlReader = sqlCmd.ExecuteReader()
GridView2.Columns.Clear()
'If there are rows of data that match are criteria
If (sqlReader.HasRows) Then
'The rows of data are grabbed for the specific centre from the database using the data reader.
GridView2.DataSource = sqlReader
GridView2.DataBind()
GridView2.Columns.Clear()
Else
MsgBox("The centre code provided does not exist...")
EndIf
'This is closing the connection to the database once we have finished with it.
sqlConn.Close()
'This is to clear the list of items out of the GridView box.
EndIf
End Sub
Property GridViewSortDirection() AsString
Get
If IsNothing(ViewState.Item("GridViewSortDirection"))Then
Return"desc"
EndIf
Return ViewState.Item("GridViewSortDirection")
EndGet
Set(ByVal Value AsString)
ViewState.Item("GridViewSortDirection") = Value
EndSet
EndProperty
Function GetSortDirection() AsString
Dim GridViewSortDirectionNew AsString
SelectCase GridViewSortDirection
Case"DESC"
GridViewSortDirectionNew = "ASC"
Case"ASC"
GridViewSortDirectionNew = "DESC"
Case Else
GridViewSortDirectionNew = "DESC"
EndSelect

GridViewSortDirection = GridViewSortDirectionNew
Return GridViewSortDirectionNew
EndFunction
ProtectedSub gv_PageIndexChanging1(ByVal sender AsObject,ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
GridView2.PageIndex = e.NewPageIndex
GridView2.DataBind()
EndSub
ProtectedSub GridView_Sorting1(ByVal sender AsObject,ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs)
Dim myPageIndex AsInteger = GridView2.PageIndex
Dim mySortdirection AsString = GetSortDirection()
Dim dv AsNewDataView()
dv.Table = GridView2.DataSource
dv.Sort = e.SortExpression & " " & mySortdirection
GridView2.DataSource = dv
GridView2.DataBind()
GridView2.PageIndex = myPageIndex
EndSub
</script>

For my GridView:
<asp:GridViewID="GridView2"runat="server"Height="143px"AllowSorting="true"OnSorting="GridView_Sorting1"


Many thanks,

Dan

 
Back
Top