Reputation: 492
The short version:
Is there a way I can add properties and override the OnMouseDown / OnMouseUp functionality on the DataGridView control without creating my own control which extends the DataGridView?
The long version (with an explanation):
I am implementing drag and drop moving of multiple rows between grids in an existing application. I have an extended DataGridView control with the functionality I require, and it works perfectly moving rows between instances of this grid.
This is the extended DGV class code:
public partial class DragDropGrid : DataGridView
{
/// <summary>
/// When set, the mouse down event and click events don't happen until the mouse button is released.
/// </summary>
public bool DelayMouseDown = false;
public int MouseDownRowIndex = -1;
protected override void OnMouseDown(MouseEventArgs e)
{
if (DelayMouseDown)
{
return;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (DelayMouseDown)
{
base.OnMouseDown(e);
}
base.OnMouseUp(e);
}
}
However, I also have another grid (a custom UserControl) which I need to be able to handle the same drag and drop functionality. This grid is a composite user control from our base class suite, and has a DataGridView along with a bunch of other stuff sitting on the control.
I have tried the following to implement the same functionality as the base DataGridView I have extended successfully, but have had no luck getting it to work:
Extending my base class grid the same as the DataGridView. This doesn't work as overriding the MouseDown / MouseUp methods does so on the custom UserControl, not the DataGridView which sits on the control - so clicking the grid doesn't fire the overridden methods.
Updating the base class grid with the properties / overridden methods.
This has the same problem as above; the MouseDown / MouseUp is
overriding the UserControl's methods, so clicking the DataGridView on
the control doesn't hit the overridden methods.
I was hoping to just add the properties and override the methods on the whole DataGridView class, so that the DGV on the custom UserControl would pick up the functionality alongside the other DataGridView controls.
Help!
Upvotes: 3
Views: 1946
Reputation: 63732
It seems that your user control has a DataGridView
inside anyway.
So instead of binding the existing MouseDown
/MouseUp
events etc. (which indeed are events of the user control, not the grid), just add new events to your user control, and let them pass down to the grid view. For example:
public event MouseEventHandler GridMouseDown
{
add { dataGrid.MouseDown += value; }
remove { dataGrid.MouseDown -= value; }
}
EDIT:
Okay, since you ask specifically about overriding the OnMouseDown
methods, the answer is simple: creating a derived type is the only way.
There are some very dirty hacks you could use to get around this, but that's a bad idea for something as trivial as this.
Upvotes: 2