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

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
        Else

            If Me.CurrentSortArray(0) = lb.ID Then
                If CInt(Me.CurrentSortArray(2)) = 2 Then
                    lb.Text = lb.Text + " " + "↑"
                Else
                    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, " ↓", "")
        Else
            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
        gridview.DataBind()
        formatGridView(_ID)

    End Sub

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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