首页 > 代码库 > Winform DataGridView鼠标右键选择列

Winform DataGridView鼠标右键选择列

1、设置DataGridView 的属性 SelectionMode

SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
2、设置事件CellMouseDown
private void dgvResult_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.dgvResult.ClearSelection();
                this.dgvResult.CurrentRow.Selected = false;
                this.dgvResult.Rows[e.RowIndex].Selected = true;
                this.dgvResult.CurrentCell = dgvResult.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }





Winform DataGridView鼠标右键选择列