Clicking Datagrid header to sort column

Posted on 2006-03-21
Last Modified: 2008-01-09
Hi all,

I have a DataGridView which displays the contents of a table and I can click on the column headers to sort by each of the columns, but what is the code for doing this?

What I mean is, I would like to sort by the second column by clicking a button instead of the column heading. How can I do it?

Question by:Freerider
    LVL 34

    Accepted Solution

    LVL 6

    Assisted Solution


    If you are not using NET2.0 then you cannot access the DataGrid.Sort and you will have to use DataViews to sort the underlying data that you are binding to.

    Here is a link that shows how to implement sorting and searching within the datagrid

    HTH :)

    Author Comment


    I am using .NET 2.0 - VB2005 Express Edition. I tried using the code from MS link, but I get a few errors.

    I managed to get rid of most of them, but still have problems with any line containing


    If oldColumn Is newColumn AndAlso dgPayments.SortOrder = sortorder.Ascending Then
                    direction = System.ComponentModel.ListSortDirection.Descending

                    ' Sort a new column and remove the old SortGlyph.
                    direction = System.ComponentModel.ListSortDirection.Ascending
                    oldColumn.HeaderCell.SortGlyphDirection = sortorder.None
                End If

    returns error " 'Ascending' is not a member of 'String' " & " 'None' is not a member of 'String' "

    Any ideas?
    LVL 6

    Expert Comment

    Why would you want to sort a column by clicking a button instead of the column header?  Just curious.

    Author Comment


    I noticed when I added new records to the dataset they were sorting in the wrong order (the last entry wasn't last in the grid). Your question prompted me to check the SQL statement and noticed it was sorting by Date and not Invoice_ID (which is what I need)

    The sort order can be changed using the column headers, but I need to make sure the datagrid is being sorted by Invoice_ID before I can identify the last entry, so I thought the best way of doing it was to use code to sort the datagrid. There might be a better way?...
    LVL 6

    Expert Comment

    Did you add 'ORDER BY Invoice_ID' to your SQL Statement?

    Author Comment

    Yes. Previously it was 'ORDER BY Date_Completed'
    LVL 6

    Assisted Solution

    Try this:

            datagridview.Columns(0).HeaderCell.SortGlyphDirection = SortOrder.Ascending

    where 0 = the index of the column you want sorted.

    This is an example given for SortGlyphDirection in the VS2005 Help:

    Public Property SortGlyphDirection As SortOrder
    Dim value As SortOrder
    value = instance.SortGlyphDirection

    instance.SortGlyphDirection = value


    Author Comment

    I gave up trying the suggestions as I kept getting errors and didn't think it was important enough to persevere.

    Actually, if the datagrid is sorted by Invoice_ID, then any new records will be added to the end of the datagrid.
    So, my original question is really irrelevant (and I don't feel was answered)
    However, I think it would be fair to split the points with those who offered help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    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

    17 Experts available now in Live!

    Get 1:1 Help Now