简体   繁体   中英

How to disable a context menu when clicking on DataGridView header

I've created a context menu and named it GridMenu, I then set the ContextMenuStrip property of a DataGridView to GridMenu. I only want the context menu to show when right-clicking on the data rows of that DataGridView. Not its column headers.

So how do I disable the context menu if any of the column headers are right-clicked?

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
        if (DataGridView1.CurrentRow.Index < 0)
            e.Cancel = true;
}

Write this in your Form load event:

 foreach (DataGridViewRow gridViewRow in dataGridView1.Rows)
        {
            gridViewRow.ContextMenuStrip = contextMenuStrip1;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM