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:
Otherwise, remove it from the below code:
private void GridViewCellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
if (e.Button == MouseButtons.Left)
{
CustomerGridView.ClearSelection();
}
}
}
Hope this tip will help you.