Solved

dbGrid Row Highlighting

Posted on 1998-06-16
7
537 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
[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
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
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

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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