0

In DataGridView (C# WinForm app) I have programmed, if a row is selected and you right click on a RowHeader, the ContextMenuStrip is shown. It was meant the ContextMenuStrip can show only if you click only on the RowHeader:

private void myDataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right && e.RowIndex > -1)
    {
        if (myDataGridView.Rows[e.RowIndex].Selected == true)
        {
            if (myDataGridView.Rows[e.RowIndex].HeaderCell.ColumnIndex == e.ColumnIndex)
            {
                DataGridView ctrl = (DataGridView)sender;
                ctrl.ContextMenuStrip = this.cmsBrisiPrekinitev;
                ctrl.ContextMenuStrip.Show(e.Location);
            }
        }
    }
}

Now, on the first row selection and right click on the RowHeader it works fine, but after that the ContextMenuStrip is shown no matter where inside the DataGridView I do right click and I can't catch any event. An event for the left mouse click is raised properly.

How can I avoid this ContextMenuStrip showing and what could be a reason for that?

Plemic
  • 1
  • 1
    Handle the `DataGridView.MouseDown` event instead and check the `DataGridView.HitTest` property. `if (e.Button == MouseButtons.Right) myDataGridView.ContextMenuStrip = myDataGridView.HitTest(e.X, e.Y).Type == DataGridViewHitTestType.RowHeader ? cmsBrisiPrekinitev: null; else myDataGridView.ContextMenuStrip = null;` –  Feb 23 '20 at 12:49
  • Related: https://stackoverflow.com/questions/6761320/context-menu-for-datagridview-cell-rowheader-and-columnheader –  Feb 23 '20 at 12:51

0 Answers0