Let's assume, we need to change attributes of the text and a background of a line in component TDBGrid if value of any field satisfies to in advance given condition. For this purpose it is accepted to use the handler of event OnDrawColumnCell of this component. We mark that the possibilities given at its usage, are rather various.
Let's consider the elementary application with TDBGrid, containing one component TTable, one component TDataSource and one component TDBGrid: we Install values of their properties according to the table resulted more low:
Component Property Value
Table1 DatabaseName BCDEMOS (or DBDEMOS)
TableName events.db
Active true
DataSource1 DataSet Table1
DBGrid1 DataSource DataSource1
Fig. 1 Test application at a design stage
Normally for image copying in cells method OnDrawColumnCell is used.
Its parameter Rect - the structure describing a rectangle occupied with a cell, parameter Column - column DBGrid in which it is necessary to change a method of drawing of the image. For a text output method TextOut of property Canvas of component TDBGrid is used.
Let's assume, we need to change text color and a background of a line depending on value of any field (for example, VenueNo). We create the handler of event OnDrawColumnCell of component DBGrid1. In case of a C ++ Builder it looks like:
void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
if (Table1-> FieldByName ("VenueNo")-> Value == 1)
{
DBGrid1-> Canvas-> Brush-> Color=clGreen;
DBGrid1-> Canvas-> Font-> Color=clWhite;
DBGrid1-> Canvas-> FillRect (Rect);
DBGrid1-> Canvas-> TextOut (Rect.Left+2,Rect.Top+2,Column->Field->Text);
}
}
In case of Delphi the appropriate code looks like:
procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Table1.FieldByName (' VenueNo ').Value=1) then begin
with DBGrid1.Canvas do begin
Brush. Color: = clGreen;
Font. Color: = clWhite;
FillRect (Rect);
TextOut (Rect. Left+2, Rect. Top+2, Column. Field. Text);
end;
end;
end;
As a result at a performance stage at display of lines in which field value VenueNo is equal 1, the background of cells will be colored in green color, and the text will be deduced by white color.
Fig. 2 Change of background color and a font in lines with field value VenueNo=1 at a performance stage.
At an output of the selected lines all data in cells appeared left-justified. If we want to display more correctly text alignment in a column, it is necessary to modify slightly our code, considering value of property Alignment leaking (that is drawn at present) columns:
void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
if (Table1-> FieldByName ("VenueNo")-> Value == 1)
{
DBGrid1-> Canvas-> Brush-> Color=clGreen;
DBGrid1-> Canvas-> Font-> Color=clWhite;
DBGrid1-> Canvas-> FillRect (Rect);
if (Column-> Alignment == taRightJustify)
{
DBGrid1-> Canvas-> TextOut (Rect. RIGHT-2
DBGrid1-> Canvas-> TextWidth (Column-> Field-> Text),
Rect. Top+2, Column-> Field-> Text);
}
else
{
DBGrid1-> Canvas-> TextOut (Rect.Left+2,Rect.Top+2,Column->Field->Text);
}
}
}
The appropriate code for Delphi looks like:
procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Table1.FieldByName (' VenueNo ').Value=1) then begin
with DBGrid1.Canvas do begin
Brush. Color: = clGreen;
Font. Color: = clWhite;
FillRect (Rect);
if (Column. Alignment=taRightJustify) then
TextOut (Rect. RIGHT-2 TextWidth (Column. Field. Text),
Rect. Top+2, Column. Field. Text)
else
TextOut (Rect. Left+2, Rect. Top+2, Column. Field. Text);
end;
end;
end;
In this case text alignment in columns coincides with alignment of columns.
Let's mark that value of offset (in this case 2 пиксела), generally speaking, depends on typeface and the type size used in the given column, and should steal up individually.
Fig. 3 Change of color taking into account text alignment in columns.
If it is necessary to display in the non-standard image not all the line long but only some cells, it is necessary to analyze a name of the field displayed in the given column, as in the event handler resulted more low. The example for a C ++ Builder looks so:
void __ fastcall TForm1:: DBGrid1DrawColumnCell (TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
if ((Table1-> FieldByName ("VenueNo")-> Value == 1) AND
(Column-> FieldName == "VenueNo"))
{
DBGrid1-> Canvas-> Brush-> Color=clGreen;
DBGrid1-> Canvas-> Font-> Color=clWhite;
DBGrid1-> Canvas-> FillRect (Rect);
DBGrid1-> Canvas-> TextOut (Rect. RIGHT-2
DBGrid1-> Canvas-> TextWidth (Column-> Field-> Text),
Rect. Top+2, Column-> Field-> Text);
}
}
The appropriate code for Delphi looks like:
procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Table1.FieldByName (' VenueNo ').Value=1) and
(Column. FieldName ='VenueNo ') then begin
with DBGrid1.Canvas do begin
Brush. Color: = clGreen;
Font. Color: = clWhite;
FillRect (Rect);
TextOut (Rect. RIGHT-2 TextWidth (Column. Field. Text),
Rect. Top+2, Column. Field. Text)
end;
end;
end;
As a result selected there are only cells for which the conditions selected by us are satisfied.