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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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?
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.