jetty
jetty

Reputation: 869

Selecting multiple rows with Shift + Click and Shift + KeyDown on DBGrid

I need to make code changes to my existing DBGrid component, such that rows are selected on Shift + Click and Shift + KeyDown. Here is what I have done so far for KeyDown:

if (Key = VK_DOWN)  and (ssShift in Shift) then
begin
       if not myDataset.Eof then
       begin
         myDataset.Next;
       end;
end;

But the above code does not select the entire row. It will only highlight the cell below it. How do i modify this code, to select the entire row? How can i do this for Shift+Click too?

Upvotes: 0

Views: 2114

Answers (1)

MartynA
MartynA

Reputation: 30745

You need to set a couple of Options in your DBGrid:

  • dgRowSelect = True

  • dgMultiSelect = True

dgRowSelect means that the entire row, rather than a single cell, is selected. The downside of this, which may not matter to you, is losing the ability to do an in-place edit in the grid.

Once you've made those changes, you'll find you don't need the code in your KeyDown handler. In fact if you leave it in, you'll find that it skips every other row so you end up with a zebra-striped grid.

`dgMultiSelect' allow you to extend the select from a single row to more than one, by holding the Shift key at the same time as the Down or Up key.

However, you now say that you want in-place editing, which is incompatible with setting dgRowSelect to True. The problem with that is that the scope for custom-drawing the cells of a DBGrid are somewhat limited without going to a lot of trouble, ime. However, the following may be acceptable for your purpose.

In your grid, make sure DefaultDrawing is set to True and that the grid's Options are set like this:

Options = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]

You should find that when you click a row, the entire row is drawn "highlighted", not just the focused cell. However, the in-place editor still works on the focused cell and you can still extend the row selection to more than one row using Shift + Up or Down.

Doing something with Shift + Click is, err, trickier. I assume you want to extend the selection from the current row to the row you Shift-Click in? I think you should post a separate q about that, because it's a different point.

Upvotes: 1

Related Questions