C# 右键单击以选择 dataGridView 中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16765477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:43:19  来源:igfitidea点击:

Right click to select row in dataGridView

c#selectdatagridviewrowright-click

提问by user2396911

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

我需要在显示 ContextMenu 之前右键单击 dataGridView 中的一行,因为 contextMenu 是行相关的。

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..

这有效,但是当我尝试读取 dataGrid.Rows[CurrentRow.Index] 时,我只看到左键单击选择的行,而不是右键单击选择的行。

采纳答案by Gjeltema

Try setting the current cell like this (this will set the CurrentRowproperty of the DataGridViewbefore the context menu item is selected):

尝试像这样设置当前单元格(这将设置上下文菜单项被选中之前的CurrentRow属性DataGridView):

    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();
        }

    }

回答by David Wakeman

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.

我意识到这个线程很旧,我只想添加一件事:如果您希望能够在多行上选择并执行操作:您可以检查您右键单击的行是否已被选中。这样,DataGridview 在这方面的行为类似于 ListView。因此,右键单击尚未选择的行:选择该行并打开上下文菜单。右键单击已选择的行只会为您提供上下文菜单并按预期保留所选行。

 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);
            }
        }
    }

回答by Mehmet Lütfi DüLKAR

    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;
        }


    }

回答by H2Five

 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

这是我猜的准确方法