Board index » delphi » Dbgrid Cells Highlighted

Dbgrid Cells Highlighted

Greetings,

How can I highlight a single data cell of a dbgrid
so that the edge around the boarder of the cell is colored or highlighted.
I've seen a code example of this sometime ago
but lost it. If possible a small code example would be great
Thanks for any help

--
Dean Hastas
Investrak Support

 

Re:Dbgrid Cells Highlighted


Quote
"dean hastas" <dhas...@yahoo.com> wrote:
>How can I highlight a single data cell of a dbgrid
>so that the edge around the boarder of the cell is colored or highlighted.
>I've seen a code example of this sometime ago
>but lost it. If possible a small code example would be great

Use the OnDrawDataCell or OnDrawColumnCell (depending on your version
of Delphi) to hande the drawing.

Good luck.

Kurt

Re:Dbgrid Cells Highlighted


Hi Dean!

This is code I used:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  OutRect : TRect;
  ConditionalField : TField;
  HighlightField : TField;
begin
  if Edit_Value.Text <> '' then begin
    OutRect := Rect;
    ConditionalField := Table1.FieldByName(ComboBox_ConditionalField.Text);
    HighlightField := Table1.FieldByName(ComboBox_HighlightField.Text);
    if ComboBox_Operator.ItemIndex = 0 then begin
      if (Column.Field = HighlightField) and (ConditionalField.AsVariant =
Edit_Value.Text) then
        DBGrid1.Canvas.Font.Color := ColorBox_HighlightColor.Selected;
    end;
    if ComboBox_Operator.ItemIndex = 1 then begin
      if (Column.Field = HighlightField) and (ConditionalField.AsVariant <
Edit_Value.Text) then
        DBGrid1.Canvas.Font.Color := ColorBox_HighlightColor.Selected;
    end;
    if ComboBox_Operator.ItemIndex = 2 then begin
      if (Column.Field = HighlightField) and (ConditionalField.AsVariant >
Edit_Value.Text) then
        DBGrid1.Canvas.Font.Color := ColorBox_HighlightColor.Selected;
    end;
    if ComboBox_Operator.ItemIndex = 3 then begin
      if (Column.Field = HighlightField) and (ConditionalField.AsVariant <>
Edit_Value.Text) then
        DBGrid1.Canvas.Font.Color := ColorBox_HighlightColor.Selected;
    end;
    DBGrid1.DefaultDrawDataCell(OutRect, Column.Field, State);
  end;
end;

As you see I used the DrawColumnCell-event on the DBGrid-component. I used a
ComboBox (where I on the TForm.Create-event listed all fields from the
TTable1-component) to set which field I want to use as the conditional
field. Then I used another ComboBox to set which field I want to highlight.
(That gives the possibility to set the condition on one field but highlight
another field. This could be very useful if you want to highligt all orders
from a specific customer. Then you use 'CustomerID' as the conditional field
but 'OrderNo' as the highlight field.)

The operator is a bit more tricky. Also there I used a ComboBox there the
string list looks like:

=
<

Quote

<>

Dependning on whick item selected in this CombBox defferent statement will
be used.

Finally I used a TColorBox-component to choose in which color I want to see
the highligtend values.

I hope this has give you some idea of how you can highlight the DBGrid.

Niklas Larsson
Digital Cowboy

"dean hastas" <dhas...@yahoo.com> skrev i meddelandet
news:3ed58d30@newsgroups.borland.com...

Quote
> Greetings,

> How can I highlight a single data cell of a dbgrid
> so that the edge around the boarder of the cell is colored or highlighted.
> I've seen a code example of this sometime ago
> but lost it. If possible a small code example would be great
> Thanks for any help

> --
> Dean Hastas
> Investrak Support

Re:Dbgrid Cells Highlighted


Use OnDrawColumnCell event:
begin
  if <somecondition> then begin
    Grid.Canvas.Color := clRed;
    Grid.Canvas.FillRect(Rect);
  end;
end;

--
Robert Cerny
http://codecentral.borland.com/codecentral/ccWeb.exe/author?authorid=...

Quote
"dean hastas" <dhas...@yahoo.com> wrote in message

news:3ed58d30@newsgroups.borland.com...
Quote
> Greetings,

> How can I highlight a single data cell of a dbgrid
> so that the edge around the boarder of the cell is colored or highlighted.
> I've seen a code example of this sometime ago
> but lost it. If possible a small code example would be great
> Thanks for any help

> --
> Dean Hastas
> Investrak Support

Re:Dbgrid Cells Highlighted


Quote
"dean hastas" <dhas...@yahoo.com> wrote in message

news:3ed58d30@newsgroups.borland.com...

Quote
> Greetings,

> How can I highlight a single data cell of a dbgrid
> so that the edge around the boarder of the cell is colored or highlighted.
> I've seen a code example of this sometime ago
> but lost it. If possible a small code example would be great
> Thanks for any help

will that do?
//set DbGrid1.DefaultDrawing to True

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
 const Rect: TRect;  DataCol: Integer; Column: TColumn;
 State: TGridDrawState);
begin
    if (Column.FieldName = 'STATUS') and
         (Column.Field.AsString = 'FAULTY') then
      Dbgrid1.Canvas.Font.Color := clRed;
    Dbgrid1.DefaultDrawColumnCell(Rect, DataCol,
    Column, State);
end;
-----------
jacob

Other Threads