Right click to select a row in a Datagridview and show a menu to delete it

后端 未结 12 671
梦毁少年i
梦毁少年i 2020-12-02 12:00

I have few columns in my DataGridView, and there is data in my rows. I saw few solutions in here, but I can not combine them!

Simply a way to right-click on a row, i

12条回答
  •  心在旅途
    2020-12-02 12:39

    All the answers posed in to this question are based on a mouse click event. You can also assign a ContenxtMenuStrip to your DataGridview and check if there is a row selected when the user RightMouseButtons on the DataGridView and decide whether you want to view the ContenxtMenuStrip or not. You can do so by setting the CancelEventArgs.Cancel value in the the Opening event of the ContextMenuStrip

        private void MyContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            //Only show ContextMenuStrip when there is 1 row selected.
            if (MyDataGridView.SelectedRows.Count != 1) e.Cancel = true;
        }
    

    But if you have several context menu strips, with each containing different options, depending on the selection, I would go for a mouse-click-approach myself as well.

提交回复
热议问题