I am using GridView control in an aspx page. I need to implement sorting on columns such that when the user clicks on any column, if the sort order is ascending, it changes to descending and vice versa.
GridView aspx page has the following content:
In aspx.vb page, I have declared the following in Partial Public Class [Inherits Class] section:
When the data is assigned to GridView:
Following code is written for grdData gridView sorting:
The issue I am facing is that gridView has 7 columns; Employee Number, Employee Name, Employee, Employer, Avg Monthly Bal, Interest, Unit Name
Whenever I click on any column, it is sorted in descending order on first click, in ascending order on second click and so on. But this is not the case with Employee Number column. Employee Number column is not working as desired. On first click, it retains its order, then changes to descending, then ascending. For example, in a sample data, I had the following values in GridView rows for Employee Number columns:
890
2975
2592
When I clicked on Employee Number, it again showed:
890
2975
2592
Upon second click, it showed:
2592
2975
890
Upon third click, it showed:
890
2975
2592
It is the first click on Employee Number, that is the issue
GridView aspx page has the following content:
Code:
<asp:gridview id="grdData"
runat="server" BorderColor="#DEBA84" AutoGenerateColumns=false
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" AllowSorting=true OnSorting=grdData_Sorting
ShowFooter="True" HorizontalAlign=Center EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="Employee Number" HeaderText="Employee Number" SortExpression="Employee Number" FooterText="Total" />
<asp:BoundField DataField="Employee Name" HeaderText="Employee Name" SortExpression="Employee Name" />
<asp:BoundField DataField="Employee" HeaderText="Employee" SortExpression="Employee" ItemStyle-HorizontalAlign=Right DataFormatString="{0:N0}" />
<asp:BoundField DataField="Employer" HeaderText="Employer" SortExpression="Employer" ItemStyle-HorizontalAlign=Right DataFormatString="{0:N0}" />
<asp:BoundField DataField="Avg Monthly Bal" HeaderText="Avg Monthly Bal" SortExpression="Avg Monthly Bal" ItemStyle-HorizontalAlign=Right DataFormatString="{0:N0}" />
<asp:BoundField DataField="Interest" HeaderText="Interest" SortExpression="Interest" ItemStyle-HorizontalAlign=Right DataFormatString="{0:N0}" />
<asp:BoundField DataField="Unit Name" HeaderText="Unit Name" SortExpression="Unit Name" />
</Columns>
<RowStyle BackColor=White ForeColor=Black />
<FooterStyle BackColor=White Font-Bold="True" ForeColor=Maroon />
<HeaderStyle BackColor=White Font-Bold="True" ForeColor=Maroon />
</asp:gridview>
In aspx.vb page, I have declared the following in Partial Public Class [Inherits Class] section:
Code:
Private Const ASCENDING As String = " ASC"
Private Const DESCENDING As String = " DESC"
When the data is assigned to GridView:
Code:
Dim view As DataView = New DataView(tmpTable)
view.Sort = "Unit Name,Employee Number"
grdData.DataSource = view
Session("sortTable") = view.Table
grdData.DataBind()
Following code is written for grdData gridView sorting:
Code:
Protected Sub grdData_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, DESCENDING)
Else
GridViewSortDirection = SortDirection.Ascending
SortGridView(sortExpression, ASCENDING)
End If
End Sub
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
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
grdData.DataSource = dv
grdData.DataBind()
End Sub
Private Function GetData() As DataSet
Dim ds As DataSet = New DataSet
ds.Tables.Add(Session("sortTable").Copy)
Return ds
End Function
The issue I am facing is that gridView has 7 columns; Employee Number, Employee Name, Employee, Employer, Avg Monthly Bal, Interest, Unit Name
Whenever I click on any column, it is sorted in descending order on first click, in ascending order on second click and so on. But this is not the case with Employee Number column. Employee Number column is not working as desired. On first click, it retains its order, then changes to descending, then ascending. For example, in a sample data, I had the following values in GridView rows for Employee Number columns:
890
2975
2592
When I clicked on Employee Number, it again showed:
890
2975
2592
Upon second click, it showed:
2592
2975
890
Upon third click, it showed:
890
2975
2592
It is the first click on Employee Number, that is the issue