Solved

Gridview sort in vb.net

Posted on 2014-07-30
1
473 Views
Last Modified: 2014-08-02
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.
0
Comment
Question by:dshi15
1 Comment
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 500 total points
ID: 40231326
Hi,

The same way as you did for bound field. Provide the SortExpression for the template field as well.. Like

<asp:GridView ID="gdView" runat="server" AllowSorting="True" 
                                 AutoGenerateColumns="False" AllowPaging="True"
                           OnSorting="_Sorting">
                            <Columns>
                                 <asp:TemplateField HeaderText="Student Number" HeaderStyle-Wrap="false"  SortExpression="Student_Number">
                                    <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" />

Open in new window


Thanks
Rajeesh
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows ICD FFU Issue 4 38
application restarter  - for an exe that hangs 9 26
start a process from a service 3 22
Help with exporting to excel 4 27
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question