Solved

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

Posted on 2006-11-08
3
484 Views
Last Modified: 2008-02-01

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!!
0
Comment
Question by:DavidGreenfield
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 17896826
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
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 500 total points
ID: 17896858
Put this in your DataGridView resize event:

        Try
            DataGridView.Rows(DataGridView.CurrentRow.Index).Selected = False
        Catch
        End Try
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17897045
That is a better way ;-)

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

Roger
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question