Add context menu in a datagrid view in a winform application

前端 未结 3 1445
小鲜肉
小鲜肉 2020-12-11 05:37

How to show a Context Menu when you right click a Menu Item in a DataGridView ? I would like to add delete in the menu so that the entire row can be deleted . Thanks in Ad

3条回答
  •  情书的邮戳
    2020-12-11 06:00

    I know this question is quite old, but maybe someone will still have use for this. There is an Event for this, CellContextMenuStripNeeded. The following code works perfectly for me and seems less hacky than the MouseUp Solution:

    private void DGV_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
    {
        if (e.RowIndex >= 0)
        {
            DGV.ClearSelection();
            DGV.Rows[e.RowIndex].Selected = true;
            e.ContextMenuStrip = MENUSTRIP;
        }
    }
    

提交回复
热议问题