I need to write the code to select multiple rows on Shift+MouseClick, so that I am able to fill down the values. Is there any way I can do it? My DBGrid options are as follows:
dbGrid1.Options = [dgEditing, dgAlwaysShowEditor, dgTitles, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
I originally thought that a problem you would be likely to have with this is that Shift + Click is handled by the grid itself, so the time the Grid's OnMouseUp fires, the grid has already moved the dataset cursor to the row that was shift-clicked. So extending the grid selection from where it was before the shift-click would not be straightforward, because you would lack the information of where the ds cursor previously was.
However, it turns out to be quite straightforward to do. I've added some comments to the code below to explain how it works.
For simplicity, I've implemented it using a ClientDataSet with an integer ID field. The point of using an ID field is that the code uses current and prior values of the ID, in a sort of hand-over-hand way as the dataset scrolls, to do its stuff. Doing similar using bookmarks would be messier because of the need to continually allocate and deallocate them, so I leave that as an exercise for the reader.
Code:
I used a interposer for
TDBGrid class
. It works with a Ctrl+Shift keys:Default selection behavior for multiselect grids is Ctrl-Click, not Shift-Click. Using it doesn't require any specific mouse click handlers.
To implement Shift-Click instead, you have to overrule/implement the mouse click handlers of the grid.