Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom sorting of a column in a datagridview
 

Custom sorting of a column in a datagridview

I have a datagridview. It have several columns in it. It is based on a dataview.
initially I set the dataview to sort by multiple columns. Which works fine. Then on the gui if the user clicks on a column it sorts by that column, which is also fine... for now.
But on of my columns is kinda odd. The data in it is string... ## h or ## d representing a number of hours or number of days. So the default sort on that won't work... S o I want to tell my datagridview how to sort that column...
So far my searching has turned up the the event sortcompare. I put in a stub for it. And I tried setting it two different ways. One using VS properties/events window to set the event handler. and the other was to add this code to form1 constructor.

dataGridView1.SortCompare += new

 DataGridViewSortCompareEventHandler(this

.dataGridView1_SortCompare);

Neither way got my code called, (set a breakpoint in the stub).
I even tried taking out the line that set the sort on the dataView just in case... still no luck.

So my questions...
1 is sortCompare the right way to go, seems a bit large in scope since it should be called for any sort on any column, but I assume I can just check the column name, and call the default handler if it isn't my "special" column.
1.5 assuming the answer to 1 is yes, how do I call the default sorter for the other columns
2 how can I get my sortCompare replacement proc called.
  • Edited byRandellP Wednesday, July 29, 2009 7:27 PM
  •  
RandellP  Wednesday, July 29, 2009 7:25 PM
I have not used SortCompare. I have do this type of thing using LINQ. If you are using .NET 3.5 or above, you can use the technique described here:

http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx

It results in a sorted DataView that you can bind to.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, July 29, 2009 7:29 PM
I have not used SortCompare. I have do this type of thing using LINQ. If you are using .NET 3.5 or above, you can use the technique described here:

http://msmvps.com/blogs/deborahk/archive/2009/07/23/linq-sorting-a-datatable.aspx

It results in a sorted DataView that you can bind to.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, July 29, 2009 7:29 PM
Well, I'm not on 3.5, and I assume to make that work I would have to put call backs in the column headers to change the sort for the datatview... But since no 3.5 doesn't matter...

Randell
RandellP  Wednesday, July 29, 2009 7:51 PM

You can use google to search for other answers

Custom Search

More Threads

• When does set method fire?
• Debugging Stored Procedure from Dot Net
• can i copy row and it's child rows and make new row with the copied one ?
• DataGridView Autosize Row/Column
• i want to do this thing but there is some problem
• Rowfilter with wildcard
• Unable to add ubound image columns to datagridview
• Simple question about a bindingsource
• resizing of comboBox and TextBox
• Obtaining key field from a selected DataViewGrid row