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

后端 未结 12 650
梦毁少年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:46

    For completness of this question, better to use a Grid event rather than mouse.

    First Set your datagrid properties:

    SelectionMode to FullRowSelect and RowTemplate / ContextMenuStrip to a context menu.

    Create the CellMouseDown event:-

    private void myDatagridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            int rowSelected = e.RowIndex;
            if (e.RowIndex != -1)
            {
                this.myDatagridView.ClearSelection();
                this.myDatagridView.Rows[rowSelected].Selected = true;
            }
            // you now have the selected row with the context menu showing for the user to delete etc.
        }
    }
    

提交回复
热议问题