Reputation: 3822
I'm trying to override the standard sorting approach of a datagridview. How do I make the application use my function(method?) rather than the default one? This event should fire when a user clicks on a column.
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
//Sort stuff.
}
Upvotes: 1
Views: 725
Reputation: 1989
I think this topic will be very interesting for you, as it matches for your requirements.
In general there are few techniques you can use to have your custom sorting in DataGridView, depending on your requirements.
Use programmatic sorting. (you would need to call Sort manually)
Use SortCompare event, which will be called on ColumnHeaderMouseClick. (for bound DataGridView)
Create your own System.Collections.IComparer. (use for unbound datagridview)
Both are can be found at msdn page here. Also please find this page explaining when which technique can be used.
Upvotes: 3
Reputation: 62256
You can not override an event, cause event is raised after actual control method execution.
To be able to do something before you need to extend DataGrid
control and override a function inside it, like this:
a pesudocode
public class MyDataGrid : DataGridView
{
public override void ColumnHeaderMouseClick(...)
{
//insert here your code and comment last line, so base class will not call it's own implementation
base.ColumnHeaderMouseClick(...); //after execution of this, the event is reaised
}
}
Riminder: this is a basic approach of how to handle this kind of situations, you have to check if this code works in your case.
Hope this helps.
Upvotes: 0