I have a DataGridView where SelectionMode=FullRowSelect and MultiSelect=False.
When a user clicks on a row it is selected as expected. However, clicking on the same row again does not deselect the row.
How can the row selection be made to toggle between selected and unselected?
As far as I know there is no out of the box functionality that will do this.
I managed to get the effect you are asking for with the following code:
public partial class Form1 : Form
{
private bool selectionChanged;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (!selectionChanged)
{
dataGridView1.ClearSelection();
selectionChanged = true;
}
else
{
selectionChanged = false;
}
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
selectionChanged = true;
}
}
That uses the dataGridView's SelectionChanged and CellClick events, along with a class level variable holding the state of the selection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With