Highlight DataGrid Row and use Mouse Scroll Wheel on DataGrid
How to Highlight a DataGrid Row and Use Mouse Wheel to Scroll in a DataGrid
There are two features demonstrated in this program.
NB! Please follow these directions in order to see the correct results.
First is how to programmatically highlight a row in a DataGrid.
1. To have the appropriate DataGrid Row become highlighted, simply
input a last name (use Martin) in the first TextBox and click the
“Highlight Grid” Command Button. You will obtain the desired result.
2. Next Click the Clear Command button then select the
“Find and Highlight Selected Grid Item” Command Button,
and input a last name in the Input Dialog Box (type in Jones)
The next feature is using the Mouse Scroll Wheel to navigate the DataGrid itself..
3. When the program starts there is red right arrow in the upper left
corner of the DataGrid. In order to use the Mouse Scroll Button the
DataGrid must have the focus, to do this click on this Red Arrow.
This action will highlight the first row of the Grid and now you can
Use the Mouse Scroll Wheel to navigate the Grid.
Re: Highlight DataGrid Row and use Mouse Scroll Wheel on DataGrid
Hi John
A little out of date I know, but just in case you are still about.
I have a legacy program, in VB6, that now utilises your Mouse Scroll Functionality(Thank You, implemented not long after this original post) and had some recent feedback.
When you click/highlight a cell, you immediately lose mouse scroll ability; the only way to get it back is to Click in the Row or Column header.
Is this something that you have encountered? and if so I would ask your advice on a fix; as I have basically copied your functions and have struggled with attempts to work this out.
This is because, by default the DBGrid's "Marquee style" is set to "Floating Editor" which creates an invisible textbox (with blinking cursor) that garbs the focus taking it away from the Datagrid itself. By doing this, it's the textbox (and not the grid) that starts capturing the WM_WHEELSCROLL messages causing the grid not scroll.
That's why you need to click the arrow as described (to select the entire row) to return the focus to the grid itself and make the mousewheel scroll/john's trick work.
To work around this, set the MarqueeStyle to something other than "Floating Editor", thus keeping the focus on the grid, and making the Mousewheel function work more consistently; if you don't need to edit the data on the grid itself, this is an ideal solution.
(there are still Markee options that select the cell, keeping focus on the grid by default but will still give you the text editor on a double click or "enter", giving you the best of both worlds)
A word of caution: johnpc's code doesn't seem to be returning the WndProc to it's original value when the form unloads; this can cause erratic behaviours and is not a best practice.
In addition to john's (excellent)code, you should add the following line to your Form_Unload event:
SetWindowLong(DataGrid1.hWnd, GWL_WNDPROC, lpPrevWndProc )
This will restore the original WndProc so that when parts of the software start getting unloaded by VB, you can rest assured the grid is being taken care of by a WndProc, in this case its original/default WndProc.