Solved

Multiselect in a TDBGrid

Posted on 1998-01-22
6
1,207 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
Technology Partners: 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Why can't I create any project with my delphi EXE file? 6 45
DBGrid or StringGrid ? 6 120
Tvertscrollbox like a whatsapp layout 5 53
migrate this code to work on android 1 31
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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