Right click to select row in dataGridView

前端 未结 4 2135
一个人的身影
一个人的身影 2020-12-29 10:31

I need to select a row in dataGridView with right click before ContextMenu shown because contextMenu is row-dependendt.

I\'ve tried this:

 if (e.Butt         


        
4条回答
  •  无人及你
    2020-12-29 11:04

    Try setting the current cell like this (this will set the CurrentRow property of the DataGridView before the context menu item is selected):

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            var dataGrid = (DataGridView) sender;
            if (e.Button == MouseButtons.Right && e.RowIndex != -1)
            {
                var row = dataGrid.Rows[e.RowIndex];
                dataGrid.CurrentCell = row.Cells[e.ColumnIndex == -1 ? 1 : e.ColumnIndex];
                row.Selected = true;
                dataGrid.Focus();
            }
        }
    

提交回复
热议问题