I think the only "supported" way (that is, not doing something with Windows messages and generally ignoring the .NET framework) would be to call OnColumnHeaderMouseClick. Which is what the method is for, except it's protected because it's really for someone doing their own version of the control, not for any random person to start firing off events.
So you could either subclass DataGridView
and add a public method to wrap OnColumnHeaderMouseClick
or you could use reflection to call the method even though it's not public.
This is a common pattern in C#. When you write
public event EventHandler XxxEvent;
it turns into something like
private EventHandler _XxxEvent;
public event EventHandler XxxEvent
{
add { _XxxEvent += value; }
remove { _XxxEvent -= value; }
}
(plus some handling of null, don't actually use that code as-is).
XxxEvent
is not actually a delegate that can be invoked outside the class (you'll get a compiler error "The event 'ClassName.XxxEvent' can only appear on the left hand side of += or -="). And the _XxxEvent
backing field is not something you're actually supposed to know about and is private anyway. So if you want anyone to be able to inherit from your class you conventionally have a method whose name is the same as the event prefixed with "On"
protected void OnXxxEvent(EventArgs args)
{
XxxEvent?.Invoke(this, args);
}