SuppaiKamo
SuppaiKamo

Reputation: 285

How can I deselect a selected DataGridViewRow when clicking it in VB.NET?

I'm looking for a way to deselect a selected DataGridViewRow when the user clicks on it. I have tried fiddling with the various click events of the DataGridView class, but the problem is that the SelectionChanged is fired before these, which causes obvious unintended behavior when the user is clicking a row not selected.

How can I obtain the behvior I want?

Upvotes: 0

Views: 614

Answers (2)

Gary Walker
Gary Walker

Reputation: 9134

It sounds like you are try the alter standard windows select multiple object behaviour. I.e., allowing multi-select then deselecting a given row by clicking on the selected row that you want to deselect. Since the datagrid will fight you tooth and nail in trying to do this based on the standard event handling, you could handle the click event, and use mouse events to determine where they click, etc ... but this is messy and fragile.

I would recommend either teaching standard windows selection behaviour to your users or adding a "selected" column that contains a checkbox and interpreting the "truthiness" of the checkbox value column in further processing.

Upvotes: 1

ps2goat
ps2goat

Reputation: 8475

Set DataGrid.SelectedIndex = -1 in your selectionchanged event, if it was previously selected (that logic is not included here).

Upvotes: 0

Related Questions