Click column header to sort

There is a few way to implement the sort of a column when you click the header. The following is implemented by onDataBinding event of the link button.

Front page

	<asp:TemplateField HeaderText="">
			<asp:LinkButton ID="lb_DA" runat="server" CommandName="SortingGridView_A" 
			  Text = "Date Absent" OnClick="gv_sort"  OnDataBinding ="OnHeaderLinkButtonBind"/>
			<asp:Label ID="lbl_DA" runat="server" Text ='<%#DataBinder.Eval(Container.DataItem,"DateAbsent","{0:MMM d, yyyy}") %>' />
		<HeaderStyle HorizontalAlign="Left"  />
		<ItemStyle ForeColor="#999999" HorizontalAlign="Left" Width="160px" />

Backend code

    Protected Sub OnHeaderLinkButtonBind(ByVal sender As Object, ByVal e As EventArgs)
        Dim lb As LinkButton = CType(sender, LinkButton)

        If Me.CurrentSortArray Is Nothing Then
        ElseIf String.IsNullOrEmpty(Me.CurrentSortArray(0)) Then

            If Me.CurrentSortArray(0) = lb.ID Then
                If CInt(Me.CurrentSortArray(2)) = 2 Then
                    lb.Text = lb.Text + " " + "↑"
                    lb.Text = lb.Text + " " + "↓"
                End If
            End If

        End If
    End Sub

    Protected Sub gv_sort(ByVal sender As System.Web.UI.WebControls.LinkButton, ByVal e As EventArgs)
        Dim gdviewlist As List(Of MemberClient)
        Dim sortfield As String = String.Empty
        Dim arrowtype = 0

        If InStr(sender.Text, "↑") > 0 Then
            arrowtype = 1
            sortfield = Replace(sender.Text, " ↑", "")
        ElseIf InStr(sender.Text, "↓") > 0 Then
            arrowtype = 2
            sortfield = Replace(sender.Text, " ↓", "")
            arrowtype = 0
            sortfield = sender.Text
        End If

        ReDim Me.CurrentSortArray(2)
        Me.CurrentSortArray(0) = sender.ID
        Me.CurrentSortArray(1) = sortfield.Trim
        Me.CurrentSortArray(2) = arrowtype

        Session("sortarray") = Me.CurrentSortArray

        gdviewlist = DataController.sortMemberClientList(DataController.SelectClientByID(Me.ddl_client.SelectedValue), sortfield, arrowtype)

        gridview.DataSource = gdviewlist

    End Sub


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s