Solved

Multiselect in a TDBGrid

Posted on 1998-01-22
6
1,211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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