Gridview does not support swap sorting

All the help that I could find from similar posts on this issue does not work for how I configured my GridView.

I got my Gridview for dynamically created columns and was able to get sorting to work on it. My paging also works, but if I sort first and then go to another page, it will lose the sort.

What do I need to change in my Paging method to remember sorting?

Here is the code for the gridview:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then Dim curLastName As New BoundField curLastName.HeaderText = "Last Name" curLastName.DataField = "LastName" curLastName.SortExpression = "LastName" GridView1.Columns.Insert(0, curLastName) Dim curFirstName As New BoundField curFirstName.HeaderText = "First Name" curFirstName.DataField = "FirstName" curFirstName.SortExpression = "FirstName" GridView1.Columns.Insert(1, curFirstName) Dim dt As DataTable = GetData().Tables(0) Dim dv As New DataView(dt) GridView1.DataSource = dv GridView1.DataBind() End If End Sub Private Function GetData() As DataSet Dim connectionstr As String connectionstr = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString() Dim myConnection As New SqlConnection(connectionstr) Dim ad As New SqlDataAdapter("SELECT * FROM EmployeeList where lastname like 'wil%'", myConnection) Dim ds As New DataSet() ad.Fill(ds) Return ds End Function Public Property GridViewSortDirection() As SortDirection Get If ViewState("sortDirection") Is Nothing Then ViewState("sortDirection") = SortDirection.Ascending End If Return DirectCast(ViewState("sortDirection"), SortDirection) End Get Set(ByVal value As SortDirection) ViewState("sortDirection") = value End Set End Property Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Dim sortExpression As String = e.SortExpression If GridViewSortDirection = SortDirection.Ascending Then GridViewSortDirection = SortDirection.Descending SortGridView(sortExpression, "DESC") Else GridViewSortDirection = SortDirection.Ascending SortGridView(sortExpression, "ASC") End If End Sub Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String) Dim dt As DataTable = GetData().Tables(0) Dim dv As New DataView(dt) dv.Sort = sortExpression & " " & direction GridView1.DataSource = dv GridView1.DataBind() End Sub Protected Sub GridView1_PageIndexChanging(ByVal sender As [Object], ByVal e As GridViewPageEventArgs) GridView1.PageIndex = e.NewPageIndex Dim dt As DataTable = GetData().Tables(0) Dim dv As New DataView(dt) GridView1.DataSource = dv GridView1.DataBind() End Sub 

Sorting and swap work, I'm just not sure what I need to change in my swap method to remember sorting. Thanks for any help!

+4
source share
2 answers

Here's the full code, including paging, sorting, and data binding:

 Sub Page_load(sender As Object, e As EventArgs) Handles Me.Load If Not Page.IsPostBack Then CreateGridColumns() BindGrid() End If End Sub Public Property SortExpression As String Get If ViewState("SortExpression") Is Nothing Then ViewState("SortExpression") = "LastName ASC" End If Return ViewState("SortExpression").ToString End Get Set(value As String) ViewState("SortExpression") = value End Set End Property Private Sub CreateGridColumns() Dim curLastName As New BoundField curLastName.HeaderText = "Last Name" curLastName.DataField = "LastName" curLastName.SortExpression = "LastName" GridView1.Columns.Insert(0, curLastName) Dim curFirstName As New BoundField curFirstName.HeaderText = "First Name" curFirstName.DataField = "FirstName" curFirstName.SortExpression = "FirstName" GridView1.Columns.Insert(1, curFirstName) End Sub Private Sub BindGrid() Try Dim tblData = New DataTable Using sqlCon As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString.ToString()) Dim sql As String = "SELECT * FROM EmployeeList ORDER BY {0}" Dim sqlCmd = New SqlClient.SqlCommand() sqlCmd.CommandText = String.Format(sql, Me.SortExpression) sqlCmd.Connection = sqlCon Using objAdapter As New SqlClient.SqlDataAdapter(sqlCmd) objAdapter.Fill(tblData) End Using End Using GridView1.DataSource = tblData GridView1.DataBind() Catch ex As Exception ' TODO: log error ' throw End Try End Sub Private Sub GridView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging Me.GridView1.PageIndex = e.NewPageIndex BindGrid() End Sub Private Sub GridView1_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting Dim currentSortColumn, currentSortDirection As String currentSortColumn = Me.SortExpression.Split(" "c)(0) currentSortDirection = Me.SortExpression.Split(" "c)(1) If e.SortExpression.Equals(currentSortColumn) Then ' switch sort direction ' Select Case currentSortDirection.ToUpper Case "ASC" Me.SortExpression = currentSortColumn & " DESC" Case "DESC" Me.SortExpression = currentSortColumn & " ASC" End Select Else Me.SortExpression = e.SortExpression & " ASC" End If BindGrid() End Sub 
+3
source

After the "business" decided that the Web Forms application with gridview needed a certain "Page 2 for incidents, etc.". WITH ALL EVENTS, postbacks, REDIRECTS, etc .... I came to a liberating conclusion.

  • I do not support this application for some heavy company with a lot of traffic (hence my solution using a database).
  • Sessions are not very suitable for timeout and use of 2 nodes for cyclic server deployment
  • Hidden fields will be lost during various actions.
  • ViewState is lost in various actions
  • Cookies will not work with users using different computers.
  • Querystring - ugly and some problems with it.

My solution is nothing ingenious to skip all this and save a row of important data in the database table in json.

I understand that this is not what everyone wants to do, but in different situations, as with mine. It is so refreshing that it doesn’t have tons of logic checking and setting ViewState, etc.

0
source

Source: https://habr.com/ru/post/1397300/


All Articles