Solved

dbGrid Row Highlighting

Posted on 1998-06-16
7
522 Views
Last Modified: 2010-08-05
I need to visually indicate to the user what the current row is on a dbGrid (beyond the triangle indicator).  However, I must keep the sensitivity to what column is selected for various sort and other functionalities.

Delphi has the Rowselected option but at the expense of individual column sensitivity.

How can I have the best of both worlds?
0
Comment
Question by:swig
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1353275
Swig,

   Would highlighting the entire row (or even bolding all text in it) and then changing the color of the text in the selected column work? If so there are several ways to change the display format for rows and / or cells in a dbGrid depending on data values or the selected rows. Let me know if this is what your looking for and I'll post some owner-drawn dbGrid code for ya to look at.

Good luck with your project!
Pegasus
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353276
Swig,

 The best way to do it is to choose a color for your selected rows, and then use the inverted color for your 'active' row. For this you've the make an owner-drawn dbgrid, like Pegasus says. You can also look at the dbgrid of the freeware rx library. (url : rx.demo.ru)

regards, Zif.
0
 

Author Comment

by:swig
ID: 1353277
Wow, I appreciate the response, guys...

I like the idea of bolding the text for the entire row (keeping the selected column highlighted).  However, I am open to other ideas.

The users that are working with this grid are dealing with having to assess the data presented in each row, process it and scroll to the next row in a vary rapid manner.  As a result, I cannot compromise how crisp the data in the row shows up - if you understand what I mean.

Thanks again for your interest/help
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 70 total points
ID: 1353278
Swig,

 Here is some code pasted out of my application that turns a given cell red depending on it's value. Notice the declarations. This code was modified from that supplied in the Delphi Programming Problem Solver by Rugenking, highly recommended. Let me know if I can be of further assistance!

Pegasus

* * *

  private
    { Private declarations }
    {$IFDEF Win32}
    procedure dbgContractEntityDrawColumnCell(Sender: TObject;
      const Rect: TRect; Datacol: Integer; Column: TColumn;
      State: TGridDrawState);
    {$ENDIF}
    procedure Drawfield(const Value: String; const Rect: TRect;
      vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment);

{======================================================================}


procedure TfrmData_Maint.FormCreate(Sender: TObject);
begin
sDataObjectNames := TStringList.Create;

{$IFDEF Win32}
dbgContractEntity.OnDrawDataCell := NIL;
dbgContractEntity.OnDrawColumnCell := dbgContractEntityDrawColumnCell;
{$ENDIF}
end;

{======================================================================}
procedure TfrmData_Maint.dbgContractEntityDrawDataCell(Sender: TObject;
  const Rect: TRect; Field: TField; State: TGridDrawState);
begin
WITH Sender AS TDBGrid, Datasource.Dataset DO
  Begin
   If DM_DATARISK.tblCounterparty.FieldByname('CP_Default_CE').Value =
      FieldByName('RCE_CONTRACT_ENTITY').AsString then
      Drawfield(Field.DisplayText, Rect, Canvas, Canvas.Font, Field.Alignment);
  end;
end;
{======================================================================}



{$IFDEF Win32}
procedure TfrmData_Maint.dbgContractEntityDrawColumnCell(Sender: TObject;
   const Rect: TRect; Datacol: Integer; Column: TColumn;
   State: TGridDrawState);
   begin
    With Sender as TDBGrid, DataSource.DataSet DO
      Begin
   If DM_DATARISK.tblCounterparty.FieldByname('CP_Default_CE').Value =
      FieldByName('RCE_CONTRACT_ENTITY').AsString then
      Drawfield(Column.Field.DisplayText, Rect, Canvas, Column.Font, Column.Alignment);
      end;
   end;
{$ENDIF}


{======================================================================}

procedure TfrmData_Maint.DrawField(const Value: String; const Rect: TRect;
      vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment);
VAR X: Integer;
begin
X := 0;
vCanvas.Font := vFont;
vCanvas.Font.Color := clRed;
vCanvas.Font.Style := vCanvas.Font.Style + [fsBOLD];
CASE vAlignment OF
  taRightJustify: BEGIN
  SetTextAlign(vCanvas.Handle, TA_RIGHT);
  X := Rect.Right -2;
  END;
  taLeftJustify: BEGIN
  SetTextAlign(vCanvas.Handle, TA_LEFT);
  X := Rect.Left + 2;
  END;
  taCenter       : BEGIN
   SetTextAlign(vCanvas.Handle, TA_CENTER);
   X := (Rect.Right + Rect.Left) DIV 2;
   end;
end;   // end CASE
vCanvas.TextRect(Rect, X, Rect.Top+2, Value);
SetTextAlign(vCanvas.Handle, TA_LEFT);
end;

0
 

Author Comment

by:swig
ID: 1353279
Pegasus,
Sorry for the delay in getting back to you.

I am getting access violations on the call to the DrawField Procedure and I cannot figure out why.  here is a sample of what I have & how I have incorporated your suggestions...
_____________________________________________________________
  private
    { Private declarations }
    procedure DrawField(const Value: String; const Rect: TRect;
              vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment);

_________________________________________________________________

procedure TfrmIVwr.DBGridX1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
  s : string;
begin
  with TDbGrid(Sender) do begin
  .
  .
  .
   else if (datasource.dataset.fieldByName('watch_flag').AsString='W') then begin
     DrawField(column.Field.DisplayText, Rect, Canvas, Column.Font, Column.Alignment);
     Canvas.Draw((rect.left+rect.right) div 2 - 6, (rect.bottom+rect.top) div 2 -6,             imgWatch.picture.graphic);
      end
  .
  .
  .
  end;
end;
___________________________________________________
procedure TfrmIVwr.DrawField(const Value: String; const Rect: TRect;
   vCanvas: TCanvas; vFont: TFont; vAlignment: TAlignment);
var
   x: integer;
begin
     x:= 0;
     vCanvas.Font := vFont;
     vCanvas.Font.Color := clRed;
     vCanvas.Font.Style := vCanvas.font.Style + [fsBold];
     CASE vAlignment OF
       taRightJustify: begin
          SetTextAlign(vCanvas.Handle, TA_RIGHT);
          x:=Rect.Right - 2;
       end;
       taLeftJustify: begin
          SetTextAlign(vCanvas.Handle, TA_LEFT);
          x:=Rect.Right + 2;
       end;
       taCenter: begin
          SetTextAlign(vCanvas.Handle, TA_CENTER);
          x:=(Rect.Right + Rect.Left) DIV 2;
       end;
     END; //End Case
     vCanvas.TextRect(Rect, x, Rect.Top + 2, Value);
     SetTextAlign(vCanvas.Handle, TA_LEFT);
end;
0
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1353280
Swig,

  Double-check the {Private} declarations, looks like your missing the {$IFDEF} directives and the DrawColumnCell procedure/declarations. Let me know & good luck!

Pegasus
0
 

Author Comment

by:swig
ID: 1353281
Pegasus,
Remember Me?  I have been away from the office.  Sorry for the delay.  I have included the declarations and directives to be exactly as you described, but I still get the access violations as before.  

Don't mean to drag this issue on.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 98
Delphi - replicating a form 8 73
Magic Software info 18 130
Not able to call Delphi XE10 dll function from Delphi 6.0 4 26
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

774 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