Clicking Datagrid header to sort column

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?

Who is Participating?
Brian CroweDatabase AdministratorCommented:

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 :)
FreeriderAuthor Commented:

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?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

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?...
Did you add 'ORDER BY Invoice_ID' to your SQL Statement?
FreeriderAuthor Commented:
Yes. Previously it was 'ORDER BY Date_Completed'
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

FreeriderAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.