Solved

Gridview sort in vb.net

Posted on 2014-07-30
1
469 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction (All good things must come to an end (http://en.wikipedia.org/wiki/All_Good_Things...)) The original MySQL API (http://php.net/manual/en/book.mysql.php) has gone away, deprecated by PHP in Version 5.5, and removed from PHP in all curre…
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…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now