Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Custom Sorting of Databound DatagridView
 

Custom Sorting of Databound DatagridView

Hi,
I cannot find the solution:
Using a datagridview which is manually populated i can provide my own sortcompare method by implementing the SortCompare-Event.
This does not work for databound datagridviews. In this caese I have to provide the sort based on the datasource (which is a datatable in my case)
But how should I do this?
How / Where can I provide the sort / compare Methods for the datatble?

Thanks for tipps...
Alex
xxalexx  Saturday, August 22, 2009 6:13 PM
Hi xxalexx,

Here is the DataGridView.SortCompare event document.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortcompare.aspx

You can find "This event occurs only when the DataSource property is not set and the VirtualMode property value is false." in the remarks category. This is a by design feature.

I suggest you can send this user experience feedback to the Microsoft Connect, the engineer there will gather this information and send it to the product team.

Thank you for your useful feedback.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 26, 2009 2:35 AM
Put a BindingSource between your DataTable and the grid. Set the Sort property of the BindingSource.

BinaryCoder  Saturday, August 22, 2009 6:26 PM
But where is the SortCompare Event? My Problem is, that the user clicks on a caption of a column in the datagridview to sort the table according to the choosen column.
How can I control this sort which is triggered by the user via datagridview?
In unbound situations this works fine, in the databound situation the event is not fired...
Is there a sample for my problem?

Thanx
Alex
xxalexx  Sunday, August 23, 2009 7:21 AM
Hi xxalexx,

Unfortunate DataGridView.SortCompare event occurs only when the DataSource property is not set and the VirtualMode property value is false. So when you are using databinding, it won't be fired.

Based on my experience, if you use databind to submit changed data to the datatable, and also you need custom sort, you'd better still think of unbound situation. When you edit the data, you can handle the CellEndEdit event to submit the data to the datatable. Or you can put a loop throughout the rows to submit changes to the datatable when "SAVE" button is click.

If you have any problem with this suggestion, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 25, 2009 3:14 AM
Hi Kira,

thank you for the reply. But I am not happy with it. I am looking for an elegant solution to the problem.
Of course I can do without databinding and to fill the tables by hand. But why should I? I would like to know, why this event is not fired. Or better, when this event is not fired, what to do? I have a very simple standard solution, in a column a date-time value is shown (like: 2009-09-02 12:24) and when the user clicks on the column header the default sort is completely wrong. So this is nothing special, a very simple standard requirement. Databinding is a very useful and helpful approach. And I dont want to do without only because this silly sort problem. Is there really no event / hook or what ever to correct the standard sort? This is working in unbound datagridviews perfectly...

Cheers
Alex
xxalexx  Tuesday, August 25, 2009 6:36 PM
Hi xxalexx,

Here is the DataGridView.SortCompare event document.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortcompare.aspx

You can find "This event occurs only when the DataSource property is not set and the VirtualMode property value is false." in the remarks category. This is a by design feature.

I suggest you can send this user experience feedback to the Microsoft Connect, the engineer there will gather this information and send it to the product team.

Thank you for your useful feedback.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, August 26, 2009 2:35 AM
Hi Kira,

thank you for the link, i knew this documentation but i was missing the reason for this "feature" and the documentation what to do when using a datasource. I would be happy, if you can give over this question to your Microsoft collegues, or tell me the channel where to post

Thank you!
Alex
xxalexx  Monday, September 14, 2009 9:15 AM
Hi,

Microsoft Connect is the best place to send feedback.

Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework!
Kira Qian  Monday, September 14, 2009 9:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Need help with datagrid
• SQL? VB? XML? ...?!
• Combo Box Error - DataGridViewComboBoxCell value is not valid
• ComboBoxColumn binding problem
• Storing images in access database
• Assign Databinding property to a textbox
• How to Update multiple tables in database using modified data in DataGridView ??
• The given datarow is not in the current DataRowCollection.
• data from datagridview convert to excel
• DataGridView - multi-line text box cells