Solved

Multiselect in a TDBGrid

Posted on 1998-01-22
6
1,202 Views
Last Modified: 2008-02-01
I am using D1 and WIN 3.11

I want to derive a component from TDBGrid so that the user can select more than one record.  I am almost there, but my problem is that the whole row gets highlighted, not just the cell(s) within the column that the user selects.

Here is a code snippet from my component:

If (FindRecordNumber >= FRangeStart) And (FindRecordNumber <= FRangeEnd) then
   If SelectedField = HighLightedField then
      If SelectedIndex = Col-1 then
      begin
           Colour := Canvas.Brush.Color;
           Canvas.Brush.Color := ClGreen;
           Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+2,Field.AsString);
           Canvas.Brush.Color := Colour;
      end;

I probably got a bit carried away with my If statements, but I was trying to cover everything.
0
Comment
Question by:jdthedj
  • 3
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1357306
I don't fully get your problem. What do you want to select? A record (= row in a dbgrid) or a column?

Or do you just want to select the cell? Here is an example :
(Look at the header of the form. Think the way of getting the row and col selected is interesting) If I'm wrong, please specify the problem.

try it this way:

unit unit1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, DB, DBTables, Grids, DBGrids;

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Table1: TTable;
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
  private
    { Private declarations }

  public
    { Public declarations }
    procedure ShowRowCol;
  end;
  THack = class(TDBGrid);

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ShowRowCol;
begin
  caption := 'Row: ' + IntToStr(THack(DBGrid1).row) + '  ' + 'Col: ' + IntToStr(THack(DBGrid1).col);
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
  ShowRowCol;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if (Sender as TDataSource).State = dsBrowse then ShowRowCol;

end;
Procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
   If gdFocused in State then
    with (Sender as TDBGrid).Canvas do begin
      Brush.Color := clRed;
      FillRect(Rect);
      TextOut(Rect.Left, Rect.Top, Field.AsString);
    end;
end;

end.



0
 
LVL 3

Author Comment

by:jdthedj
ID: 1357307
Hi ZifNab thanks for the reply.  I am wanting to select multiple cells in one column.  I have experimented further within my component (TDBGrid derivative) and I have managed to do what I want to do, but when I call Invalidate
the system locks up unless I click an adjacent column.  If I don't call Invalidate, the highlighting disappears as soon as I release the shift key.

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357308
Mmmmm....
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Author Comment

by:jdthedj
ID: 1357309
Hi ZifNab - Mmmmmm indeed. After a lot of experimenting I have now decided that I will have to just highlight the first and last items of the requested block within the column
That at least works properly.  I still can't stop the Invalidating when I try to highlight all the required records.


0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357310
jdthedj,
I soon have to make an adhanced dbgrid too. I'll try to make what you want too.
0
 
LVL 3

Author Comment

by:jdthedj
ID: 1357311
Thanks ZifNab!!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now