We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Clicking Datagrid header to sort column

Freerider asked
Medium Priority
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?

Watch Question

Database Engineer
Top Expert 2005

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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



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?

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



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?


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


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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.