Link to home
Start Free TrialLog in
Avatar of DavidGreenfield
DavidGreenfieldFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Datagridview - always selects first row - how can I stop this


hi there

Whenever I load data into a datagridview the first row is always selected.  In VB6 days on an MSflexgrid there used to be a highlight on focus command where a row only become selected when a user clicked on the row.

How can I emulate this behaviour?

many thanks!!
Avatar of Sancler
Sancler

One way of doing this - I'm not saying it's the only one, but I haven't found a better - is to add a "dummy" column at column index 0.  Set its minimum width as 2 (which is the smallest allowed for a "visible" column) and its actual width as 2 and all its fore and backcolor properties to Control on the System Colors tab.  Then make sure that the DataGridView's SelectionMode is set to CellSelect.

What then happens is that although the top left cell is then "selected" by default, it is so small that the user can hardly see it and, in any event, its color shows no indication of that selection: it just looks like a slightly wider border on the left edge of the grid.

Then, as soon as a "real" cell is clicked, you can reset selection mode to FullRowSelect if that is what you prefer.

Obviously, you will also then need to take account of the extra column in any code which relies on column indexes.

Roger
ASKER CERTIFIED SOLUTION
Avatar of Kinger247
Kinger247

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a better way ;-)

Although it doesn't work for me in resize, it does once the binding is complete.

Roger