Home » VB.Net

Sorting by Columns with Datagridview

Is it possible to sort a datagridview by column when a user clicks a column header?  I have the sort method set to automatic under each column, which from what I read, is what I need.  I know this has to be insanely easy....

7 Answers Found


Answer 1

It actually depends on your datasource that is tied to the datagridview. What data source are you using?

Answer 2

It's a custom object that's assigned to a binding source which is then set to the datagridview.

Answer 3

Unfortunatly, you don't get any automatic sorting with the datagridview, unless the underlying data is already in some sort of sortable list.

When you said it is a custom object, I assume you are assigning some sort of array or collection of this custom object to the binding source?


Answer 4

the binding source is actually a List object where the type of list is a custom object, but still a list none the less, so I would think that it would be sortable.  What type, or how, would I have to assign a source to a datagridview so that I can click on the column headers to sort?

Answer 5

Did you check the documentation for the Sort method?




Answer 6



I know you have probably done this lol but I thought I would mention it anyways. 

If you click on the top right corner arrow of the datagridview(where you add a new data source) you will see the link, "Edit Columns".

If you click that link, a window will pop open. Select your column on the left and then on the right, in the "Behavior" heading, you should see "SortMode".

Change the SortMode to Automatic. That will allow the datagridview column to be sorted automatically when the user clicks on the column header.


Hope this helped a little.




Answer 7

Yeah, I've seen the documentation for that, but I was wondering if there was a way to do it without all the code...



The very first option is automatic which states that clicking the column automatically sorts the data.  This is what I was looking for, but setting everything to Automatic doesn't work for me.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure