Link to home
Start Free TrialLog in
Avatar of dshi15
dshi15Flag for United States of America

asked on

Gridview sort in vb.net

Hi Expert,

I have Gridview sorting work for Boundfield and not work for template field? How I can sort in template field? The header is link for Student_number

<asp:GridView ID="gdView" runat="server" AllowSorting="True" 
                                 AutoGenerateColumns="False" AllowPaging="True"
                           OnSorting="_Sorting">
                            <Columns>
                                 <asp:TemplateField HeaderText="Student Number" HeaderStyle-Wrap="false" >
                                    <HeaderTemplate>
                                    <asp:LinkButton ID="lnkStudentNumberHeader" runat="server" Text="Student Number"   CommandName="Sort" CommandArgument='<%# Eval("Student_Number") %>'>
                                    </asp:LinkButton>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                                                              <asp:LinkButton runat="server" ID="lblStudentNumber" VerticalAlign="Top" Text='<%# Eval("Student_Number") %>'
                                            OnClick="lnkStudentNumber_click" CommandName="Select" ></asp:LinkButton>
                                    </ItemTemplate>
                                                                       <ItemStyle CssClass="leftalign" VerticalAlign="Top" />
                                </asp:TemplateField>
                                <asp:BoundField DataField="First_NAME" HeaderText="First Name" ReadOnly="True" 
                                    SortExpression="First_NAME" />


Code behind:

Public Property SortDirection() 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 gdView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

            Dim sortExpression As String = e.SortExpression
            Dim direction As String = String.Empty
            If SortDirection = SortDirection.Ascending Then
                SortDirection = SortDirection.Descending
                direction = " DESC"
            Else
                SortDirection = SortDirection.Ascending
                direction = " ASC"
            End If

            Dim dt As DataTable = Me.GetDataFromdatabase.Tables(0)
            dt.DefaultView.Sort = sortExpression & direction
            gdView.DataSource = dt
            gdView.DataBind()


        End Sub

Open in new window



Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of rajeeshmca
rajeeshmca
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial