C# – How to stop a DataGridView from automatically selecting a row


I have a DataGridView control in a TabPage, and I listen for the SelectionChanged event. When the TabPage is selected, the DataGridView selects the first row and fires the SelectionChanged event. How can I stop it from automatically selecting a row?

Best Solution

I can think of two ways of working around this, If you know the event always fires, Have a bool flag that is set on the first selection, then only perform the rest of your SelectionChanged code.

The other way is to have a hidden control that is the first TabStop for the TabPage and hence is the first control to get focus (I say this without having tested any this of course, just my two cents).