Solved

Multiselect in a TDBGrid

Posted on 1998-01-22
6
1,204 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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