datagrid sort with autogeneratecolumn = false

Posted on 2005-04-16
Last Modified: 2012-06-27

How can we sort a datagrid if the grid is set to autogenerate=false? sorting ASC then DSC using mouse lcik on the column header.
The grid is being populated by data from sql server.


    Sub Sort_Grid(sender As Object, e As DataGridSortCommandEventArgs)
            Dim SortExpression As String
            SortExpression = e.SortExpression.ToString()
            If SortMode = "Desc" Then
              SortMode = "Asc"
              SortExpression = SortExpression + " " + "Desc"
              SortMode = "Desc"
            End If
            Dim odataView as DataView = GenerateSource().DefaultView
    End Sub 'Sort_Grid
Question by:EdwardPeter
    LVL 18

    Accepted Solution

    The above code will work for 2 way sort, just make sure following things:

    1) In your Datagird specify SortExpression of each column that you want to Sort:

    <asp:BoundColumn DataField="ColumnName" SortExpression="ColumnName" HeaderText="ColumnName"></asp:BoundColumn>

    2) Declare SortMode Variable as shared with your class declaration:

    Public Shared SortMode As String = "Asc"

    3) write above code in Sort Command of DataGrid.


    Author Comment


    You're simply the best. Sadly my old account had expired need to create a new one.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    779 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

    19 Experts available now in Live!

    Get 1:1 Help Now