65.9K
CodeProject is changing. Read more.
Home

Preventing Deselect (Ctrl+Click) in GridRow

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (4 votes)

Nov 28, 2013

CPOL
viewsIcon

16399

Tip for preventing deselection of rows in DataGridView

Introduction

This tip will help in preventing deselection of rows in the DataGridView control. Many would have come across this situation where-in always any random or specific row should be selected in the GridView.

Using the Code

The simple way to do that is to include the below line of code in the DataGridView CellMouseDown event. If you don't need right click option in the DataGridView control, then add this:

// if (e.Button == MouseButtons.Left)

Otherwise, remove it from the below code:

// GridViewCell Mouse Down Event
private void GridViewCellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
 // Check for Modifiers Key(CNTRL Key)
 if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
 { 
  if (e.Button == MouseButtons.Left)
  {
        CustomerGridView.ClearSelection();
  }
 }
}

Hope this tip will help you.