?
Solved

Gridview sort in vb.net

Posted on 2014-07-30
1
Medium Priority
?
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 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…

801 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