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:
=
<
<>
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