1

I want to mark a DataGridViewCellEventArgs as handled so that nothing downstream from it interferes with the way it was handled.

The DataGridViewCellEventArgs class does not have a handled property, and neither does its base class.

The event that I am working with isCellMouseEnter

This is the base DataGridView control that I am instantiating from:

public class DataGridViewWithFormatting : System.Windows.Forms.DataGridView
{
    protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
    {
        base.OnCellMouseEnter(e);
        this.Cursor = Cursors.Default;
    }
}

This is the DataGridView control that I am using in my form:

private CustomControls.DataGridViewWithFormatting dgvItems;

and...

dgvItems.CellMouseEnter += new EventHandler(dgvItems_CellMouseEnter);

then...

private void dgvItems_CellMouseEnter()
{
    this.Cursor = Cursors.Hand;
}
Marwan مروان
  • 2,163
  • 8
  • 30
  • 40
  • `DataGridViewCellEventArgs` doesn't have any `Handled` or `Cancel` property, so if you want to stop event propagation, you need to override the method which raises the event and do not call base method. – Reza Aghaei Aug 08 '19 at 06:01
  • 1
    What's the requirement, there may be some other solutions if we know the requirement. – Reza Aghaei Aug 08 '19 at 06:21
  • @JohnG I updated the description with the event (`CellMouseEnter`). – Marwan مروان Aug 08 '19 at 17:51
  • @RezaAghaei I am trying to modify the mouse cursor when it enters the cell, but the other `CellMouseEnter` event that fires after mine overrides my mouse cursor setting/change. – Marwan مروان Aug 08 '19 at 17:54
  • Could you clarify it a bit more? What kind of cell? What cursor? Also it would be great if you can share a [MCVE]. It will help us to reproduce the problem and makes sure we are on the same page, trying to solve the same problem. – Reza Aghaei Aug 08 '19 at 17:56

1 Answers1

1

You can use either of the following solutions depending on your requirements.

1 - Use BeginInvoke

To set the cursor in CellMouseEnter, you can use BeginInvoke:

private void Dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    BeginInvoke(new Action(() => dgv.Cursor = Cursors.Hand));
}

2 - Create a new cancellable(handle-able) DataGridViewCellEventArgs

Create a new MyDataGridViewCellEventArgs event args deriving from DataGridViewCellEventArgs having a Handled property. Then in your derived DataGridView, when calling base.OnCellMouseEnter, pass an instance of the new cancellable(handle-able) event args. In the event handlers, still keep the DataGridViewCellEventArgs in the signature. To cancel, cast it to MyDataGridViewCellEventArgs and cancel it by setting Handled = true;:

public class DataGridViewWithFormatting : System.Windows.Forms.DataGridView
{
    protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
    {
        var myE = new MyDataGridViewCellEventArgs(e);
        base.OnCellMouseEnter(myE);
        if (!myE.Handled)
            this.Cursor = Cursors.Default;
    }
}
public class MyDataGridViewCellEventArgs : DataGridViewCellEventArgs
{
    public bool Handled { get; set; } = false;
    public MyDataGridViewCellEventArgs(DataGridViewCellEventArgs e)
        : base(e.ColumnIndex, e.RowIndex) { }
}

And the event handler:

private void Dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
    dgv.Cursor = Cursors.Hand;
    var myE = e as MyDataGridViewCellEventArgs;
    if (myE != null)
        myE.Handled = true;
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398