making multiselections in dbgrid the windows way?

Hi, does anyone know how I should alter the dbgrid so I can make multiple selections like in the standard windows? i.e. selecting the first, holding down shift and make the last selection and all in between gets selected.
Thanks, Pascal
corpaAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
Tussin ,
you should read the questions and comments better before posting answers..


Corpa ,
is this what your after:
//multiselect set to true in object inspector

var
  Form1: TForm1;
  firstpoint, thispoint: tpoint;
  thisfield : string;
 

implementation

uses Unit2,Unit3;

{$R *.DFM}

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
if (dgmultiselect in dbgrid1.options) then
  if getkeystate(vk_shift) < short(0) then begin
   getcursorpos(thispoint);
    while not table1.eof and not table1.bof and
     not (dbgrid1.datasource.dataset.fields[0].asstring = thisfield) do     begin
     if thispoint.y > firstpoint.y then table1.prior
     else table1.next;
   dbgrid1.selectedrows.currentrowselected := true;
  end;
 end;
getcursorpos(firstpoint);
 thisfield := dbgrid1.datasource.dataset.fields[0].asstring;
end;


Regards Barry
0
 
kretzschmarCommented:
listening . . .
0
 
rwilson032697Commented:
You can already do this usig CTRL (not standard I know - but it's there...)

Cheers,

Raymond.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
intheCommented:
meikl,
i thought you had example of multiple selections using shift.
0
 
AswCommented:
listening.
0
 
kretzschmarCommented:
to barry,
so that you can assimilate it ;-))
no, have no sample yet, which will work properly, but i'm working on it.

meikl
0
 
ITugayCommented:
listening...
0
 
jeurkCommented:
how to do it with ctrl is here :
http://www.torry.webnorth.com/samples/samples/rsdtip01.zip

I'm giving the link because of the licence of the sample asking me to.

I know you are talking about a selection using shift. But it's just in case...

CU.
0
 
TussinCommented:
just add dgMultiSelect to TDBGrid.Options

you also can use this command

dbgrid1.Options:=dbgrid1.Options+[dgMultiSelect];

Now, you can hold down shift button and press down as you want.

Note. I use delphi 5
0
 
kretzschmarCommented:
hmm tussin,

this will not work, if you click one record and press shift and click another record, as you do in the explorer with files.

just to remark

meikl
0
 
kretzschmarCommented:
hi again,

there is a comercial grid, which does it and much more. take a look to:

http://www.devexpress.com/products/vcl/product.asp?prodid=10

meikl
0
 
EpsylonCommented:
www.tmssoftware.com has some nice components too.
0
 
kretzschmarCommented:
hi barry,

just to comment, not evaluated yet,
what, if the scrollbar is used, to scroll the record into view, which will end the selection-range and the thispoint.y is lower as firstpoint.y, but i've scrolled forward?

meikl
0
 
intheCommented:
mmm.. how annoying ,clicking on a scrollbar selects a record.

ok i dont like scrollbars anymore it makes this routine look a bit silly.
it still selects the records as it should but also the top or bottom record in view is selected depending on which way is scrolled.
when you try it you will see what i mean.
i suppose we could do
ShowScrollbar( dbgrid1.handle, SB_VERT, False );
and create our own scrollbar that didnt select records when clicked ..
maybe we should try making component for all this..

0
 
corpaAuthor Commented:
Tussin,
I'm sorry but your answer really isn't acceptable, but as you can see in the comments I guess you found that out allready by yourself.

Meikl,
That commercial component is just perfect, it really does the trick, but also too expencive :(

Barry,
interesting stuff, i'll play around with it a little.

In the meantime keep coming up with new idea's. I think there are more people interested in this feature as I look to the comments.

thank you very much so far,
Pascal
0
 
corpaAuthor Commented:
Hello Barry,

I just played around with your sugestion and look, I found the solution.
But one should work with the DBGridPlus component that I found on the Delphi superpage somewhere.
Try http://ftp/d20free/dbgrdpls.zip
It has this OnChangeDrawing event where you can do nice things.
No more annoying scrollbar side effects.

Ok, here's what I made of your code:

----------------------------------------------------------
var
  Form1: TForm1;
  firstpoint, thispoint: tpoint;
  thisrec,selcount : integer;

implementation

{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
begin
 //setting initial value
 selcount := 0;
end;

procedure TForm1.DBGridPlus1CellClick(Column: TColumn);
begin
 if (dgmultiselect in dbgridPlus1.options) then
  if (getkeystate(vk_shift) < short(0)) and (selcount > 0) then
   begin
    getcursorpos(thispoint);
    table1.DisableControls;
    while not table1.eof and not table1.bof and
          not (table1.RecNo = thisrec) do
       begin
         if thispoint.y > firstpoint.y then table1.prior
         else table1.next;
         dbgridPlus1.selectedrows.currentrowselected := true;
       end;
    table1.EnableControls;
   end
   else
   begin
    getcursorpos(firstpoint);
    thisrec := table1.RecNo;
   end;
 selcount := dbgridPlus1.selectedrows.Count;
end;

procedure TForm1.DBGridPlus1ChangeDrawing(Sender: TObject; DataCol,
  DataRow: Integer; AState: TGridDrawState; var Highlighted: Boolean);
begin
  Highlighted := (sender as TDBGridPlus).selectedrows.currentrowselected;
end;
----------------------------------------------------------

I will give you half the credit since you leaded me on track, ok.

Regards, Pascal
0
 
corpaAuthor Commented:
Wel if the link to DBGridPlus doesn't work, let me know and I can mail it to ya.

Pascal
0
 
intheCommented:
hi,
looKs good would you mind i try that component ,the ftp seems always busy so hope you can send.

Inthe@Future-Software-House.co.uk

cheers
  Barry

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.