Right click to select row in dataGridView

隐身守侯 提交于 2019-11-30 13:01:50

问题


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.Button == MouseButtons.Right)
        {

            var hti = dataGrid.HitTest(e.X, e.Y);
            dataGrid.ClearSelection();
            dataGrid.Rows[hti.RowIndex].Selected = true;
        }

or:

private void dataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            dataGrid.Rows[e.RowIndex].Selected = true;
            dataGrid.Focus();
        }
    }

This works but when I try to read dataGrid.Rows[CurrentRow.Index] I see only the row selected with left click and not those selected with right click..


回答1:


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)
    {
        if (e.Button == MouseButtons.Right)
        {
            // Add this
            dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex]; 
            // Can leave these here - doesn't hurt
            dataGrid.Rows[e.RowIndex].Selected = true; 
            dataGrid.Focus();
        }

    }



回答2:


I realize this thread is old, I just wanted to add one thing: If you want to be able to select, and perform the action, on multiple rows: you can check to see if the row you are right-clicking is already selected. This way the DataGridview behaves likes a ListView in this regard. So right clicking on a row not already selected: selects this row and open the context menu. Right clicking on a row already selected just gives you the context menu and keep the selected rows as expected.

 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex != -1 && e.ColumnIndex != -1)
        {
            if (e.Button == MouseButtons.Right)
            {
                DataGridViewRow clickedRow = (sender as DataGridView).Rows[e.RowIndex]; 
                if (!clickedRow.Selected)
                    dataGridView1.CurrentCell = clickedRow.Cells[e.ColumnIndex];

                var mousePosition = dataGridView1.PointToClient(Cursor.Position);

                ContextMenu1.Show(dataGridView1, mousePosition);
            }
        }
    }



回答3:


    private void grid_listele_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            grid_listele.ClearSelection();
            grid_listele[e.ColumnIndex, e.RowIndex].Selected = true;
        }


    }



回答4:


 if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            var hti = GridView.HitTest(e.X, e.Y);
            GridView.ClearSelection();

            int index = hti.RowIndex;

            if (index >= 0)
            {
                GridView.Rows[hti.RowIndex].Selected = true;
                LockFolder_ContextMenuStrip.Show(Cursor.Position);
            }

        }

This is Accurate Method i Guess



来源:https://stackoverflow.com/questions/16765477/right-click-to-select-row-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!