Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have searched this forum and many others but am unable to correctly formulate my search question.

I have a datagridview (VS2013, .Net4, VB) loaded with data. I need to set it to sort the column 1 data programmatically. The Microsoft On-line help shows how to sort using a user-selected column, but not how to select a column when none are selected, programmatically.

First, it is even possible ?

Second, if you know how this might be done I would appreciate it greatly.
Posted
Comments
Sergey Alexandrovich Kryukov 8-Jun-15 17:12pm    
What's wrong with reading original MSDN documentation on this control? This documentation is crystal clear.
—SA

Here are the selection modes implemented for this grid view class:
https://msdn.microsoft.com/en-us/library/3c89df86%28v=vs.110%29.aspx[^].

And this is the property used to set chosen selection mode: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Please, read this: DataGridView: How to select an entire Column and deselect everything else?[^]. Sounds like you want to select entire column to sort it, but a datagridview is still using SortMode = DataGridViewColumnSortMode.Automatic. As per my understanding, you have to disable automatic sorting.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jun-15 17:52pm    
Why do you think automatic should be disabled? Sorting only works when you add data or explicitly request sorting. It does not happen when you only click the control for selection of the cell, column or row purpose.
—SA
Maciej Los 8-Jun-15 17:53pm    
Have you read thread on SO, Sergey?
Sergey Alexandrovich Kryukov 8-Jun-15 17:56pm    
I just cannot believe that sorting mode can affect selection. I can believe only if I try it myself or find on .NET FCL source code...
I hope you don't believe that SO answers are always better than CodeProject's... :-)
—SA
Maciej Los 8-Jun-15 17:59pm    
Well, i'll remove my answer if you'll prove your words ;)
[EDIT]
CP rules!
Thanks guys for your replies. I have looked at your suggestions, and they both could work. In the meantime, I kept plugging away at the issue and realize I was overthinking (Again!:()!) the issue.

The solution that works for me is as follows:
my_data_grid.Sort(my_data_grid.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900