TDBGrid Multiselect = true Undesired selection

I want to preselect all rows of the dbgrid so that if the user wants it, he can unselect some lines instead of
selecting them.

so I wrote this :

Request.DisableControls ;
Request.First;
while not Request.Eof do
begin DBGR.SelectedRows.CurrentRowSelected := true;
Request.Next;
end;
Request.EnableControls ;

so far so good. My DBGrid is correcly seen with all lines highlighted.

the problem arises when the user wants to unselect a row : he will naturally shiftclick it.

the oncellclicked event is fired and I can find out that the current row is unselected... but it remains highlighted !!!. The change will only be visible if I go to hit another row.

from me it looks like a persistant bug of Delphi. (I am running D4.03)

Does somebody has a turnaround for this?
LVL 4
mhervaisAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
kretzschmarConnect With a Mentor Commented:
hi marc,

here it comes

unit SelDBGrid;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids;

type
  TSelDBGrid = class(tdbgrid)
  private
    { Private declarations }
  protected
     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
     procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  public
    { Public declarations }
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure TSelDBGrid.WMLButtonDown(var Message: TWMLButtonDown);
begin
  if message.Keys and MK_SHIFT <> 0 then
  begin
    message.Keys := message.Keys and MK_SHIFT;      //Discard shift
    if message.Keys and MK_CONTROL = 0 then
      message.Keys := message.Keys or MK_CONTROL;   //add Control
  end;
  inherited;
end;

procedure TSelDBGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
begin
  AState := AState - [gdSelected];            //Remove SelectedState preventive
  inherited DrawCell(ACol,ARow,ARect,AState);
end;


procedure Register;
begin
  RegisterComponents('Samples', [TSelDBGrid]);
end;

end.

try it out

meikl
0
 
intheCommented:
hi,
i tried and dont have this problem though im wondering did you write a procedure for shift clicking?

i ask because for me i hold down the ctrl key to unselect and all is fine,if i hold the shift key everythng is unselected except the line i clicked..
(also running D4.03)
Regards Barry
0
 
mhervaisAuthor Commented:
No other procedure Barry.

Might be the french version of D4. I was never satisfied the way it shows the selection.

when I put back the indicator, I get a better info because the arrow is not the same depending selection or not. but the highlighing remains until I click on another row

I checked on D5 and the problem is same,

Regards, Marc
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
mhervaisAuthor Commented:
Whatever the problem comes from, maybe you know something to control explicitely the highlighting of the rows ?
0
 
intheCommented:
yep maybe should add a on click procedure to catch this and unhighlight it manually..difficult for me to test as mine already un-hightlights but i'll have  ago ;-)
0
 
intheCommented:
ok i had a go ..didnt work :-(
my idea is  along the lines of a global boolean and check on keydown event check if (ssCtrl in shift)
(that means the control key is down) and if it is set the boolean to true and on mouse down check the boolean and if the boolean is true unselect the clicked row ..something like that ..
im off too sleep on it ;-)
maybe someone else has idea
good luck

0
 
kretzschmarCommented:
hi mhervais,

? isn't it so as in the explorer ?
use ctrl-click to unselect
instead of shift-click

meikl
0
 
mhervaisAuthor Commented:
Actually, in the explorer and in my grid,
ctrl click really unselects. if I catch the clicked event and if I look if it is selected or unselected, this part of the work is ok.

what I don't like, is that the line remains highlighted just like if nothing had happened.

the higlighting should stop as the line is unselected, and should rehappen when the selection occurs again, even if I remain all day clicking on the same line.

and this actually doesn't work

regards, Marc
0
 
kretzschmarCommented:
hi again mhervais,
(just gone from the other thread)
well, depending for what you need the selectedrows you can overide the painting in an descanded object, but the painting retrieves the bookmarkstr for painting. if you will now enable the shiftkey for deselect one, this will normally cause to deselect all and select this one. therefore my idea was to make a copy of the tbookmarkstr(selected rows), unselect there the shift-clicked entry and copy back (which does not work, because the selectedrows-property is a readonly-property).

if you do a descendant object of the tdbgrid, then there are some possibilities to solve it (for ex. the painting, you've spoken for, also to do a read/write selectedrows-property, or a redirecting of th shift-state into a control-state or a combination of this or some other)

meikl

0
 
mhervaisAuthor Commented:
Adjusted points to 150
0
 
mhervaisAuthor Commented:
Increased the points to 150.
do you have an example for repainting?
0
 
kretzschmarCommented:
well,
i will see what i can do ;-)

just for rephrasing
- you select all records in the dbgrid
- if the user do a shift-click
  - the clicked record will be unselected/selected
  - if unselected the record-row  should be not highlited, also if it be the currentrecordrow

right?

meikl
0
 
mhervaisAuthor Commented:
- you select all records in the dbgrid
yes
- if the user do a shift-click

shift or control click

  - the clicked record will be unselected/selected

  - if unselected the record-row  should be not highlited, also if it be the currentrecordrow

exactly.

regards meikl, marc

0
 
kretzschmarCommented:
well ok,
it will need a bit time,
(didn't have much,
too many work is waiting for me)
therefore be patient

i guess 1-2 days for a result

meikl
0
 
mhervaisAuthor Commented:
take your time Meikl, Ia overloaded too


regards, Marc
0
 
mhervaisAuthor Commented:
Youve done it Meikl. Very good.

I wonder why Borland did not remove this one !

You say the bug does not happens in the english version?

regards,

Marc
0
 
kretzschmarCommented:
hi marc,

glad that this helps you :-)

>You say the bug does not happens
>in the english version?

did i said this ?
what for a bug ?

i know about this behaviour and its
the same in the english version also
in the german version, also i don't
think thats a bug.

its just so, because the currentrecordrow is normally highlighted if rowselect enabled
in the grid-options, nevertheless its
bookmarked or not.

good luck again

meikl

0
 
mhervaisAuthor Commented:
I understand Nevertheless is is not natural for a user

Ihad some instability problems in Delphi
after running your component int my
program

therefore I found more protective to do this :

procedure TSelDBGrid.WMLButtonDown(var Message: TWMLButtonDown);
begin
  if (ComponentState = []) or (ComponentState = [csDestroying])  then
  begin
    if message.Keys and MK_SHIFT <> 0 then
    begin
      message.Keys := message.Keys and MK_SHIFT;      //Discard shift
      if message.Keys and MK_CONTROL = 0 then
        message.Keys := message.Keys or MK_CONTROL;   //add Control
    end;
  end;
  inherited;
end;

procedure TSelDBGrid.DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
begin
  if (ComponentState = []) or (ComponentState = [csDestroying])  then
  AState := AState - [gdSelected];            //Remove SelectedState preventive
  inherited DrawCell(ACol,ARow,ARect,AState);
end;


regards, Marc
0
 
mhervaisAuthor Commented:
I cannot say that the instability came from your component though. But I thout it was better to ensure that the component modification would occur only during execution.

regards, Marc
0
 
kretzschmarCommented:
well,
sure marc, it is just a prototyping therefore there can be some unknown effects, be free to modify.

remark: didn't recognized instability on my tests, but i also don't know in which context u use the grid.

maybe it will be usefull to add a switch-property (Boolean) to toggle on/off the new behaviour (if off, then work as the normal dbgrid)

hope you have no further problems with it

meikl
0
 
mhervaisAuthor Commented:
I don't know yet .  Thanks very much anyway

regards,

Marc
0
All Courses

From novice to tech pro — start learning today.