Solved

Gridview sort in vb.net

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

691 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