in
Delphi
Language database programming,
DBGRID
It is one of the main means of displaying data. but
DBGRID
The default appearance has not seen monotonous and lack of ideas. In fact, we can fully achieve it in our program.
DBGRID
The purpose of the appearance. We can change by programming
DBGRID
Head, grid, grid line foreground color and background color, as well as the size and style of the related font.
The following sample program demonstrates
DBGRID
The setting of each attribute make
Delphi
The displayed form is as beautiful as the form in the web page.
The sample program is running:
in
Form1
Placed
DBGRID1
,
Query1
,
DataSource1
Three database components, setting related properties, make
DBGRID1
Can display the data in the table. Then
DBGRID1
of
OnDrawColumncell
Type the following code in the event, then run the program, you can see the magical result. This code
WINDOWS98
,
Delphi5.0
The environment is debugged.
procedure TMainForm.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i: integer; begin if gdSelected in State then Exit; //
Define the fonts and background colors of the header:
For i: = 0 to (sender as tdbgrid) .COLUMNS.COUNT-1 Do Begin (Sender AS TDBGRID) .COLUMNS [i] .title.font.name: = '
Song body
'; //
Font
(Sender as tdbgrid) .COLUMNS [i] .title.font.size: = 9; //
font size
(Sender as tdbgrid) .COLUMNS [i] .title.font.color: = $ 000000FF; //
font color
(
red
(Sender as tdbgrid) .COLUMNS [i] .title.color: = $ 0000FF00; //
Background color background
(
green
End; //
Valley change grid background color:
If query1.recno mod 2 = 0 THEN (Sender as tdbgrid) .canvas.brush.color: = clinfobk //
Define background color
Else (Sender as TdbGrid) .canvas.brush.color: = RGB (191, 255, 223); //
Define background color
//
Define the color of the grid line:
DBGRID1.DEFAULTDRAWCOLUMNCELL (Rect, Datacol, Column, State); with (Sender AS TdbGrid) .canvas DO //
painting
Cell
Border
Begin Pen.color: = $ 00ff0000; //
Define brush color
(
blue
) MoveTo (Rect.Left, Rect.Bottom); //
Paint pen
LINETO (Rect.right, Rect.Bottom); //
Draw blue horizontal line
Pen.color: = $ 0000FF00; //
Define brush color
(
green
) MoveTo (Rect.right, Rect.top); //
Paint pen
LINETO (Rect.right, Rect.Bottom); //
Painted green vertical line
END;
2.Delphi5 - Interlaced DBGRID Grid Color:
Place DBGRID1, Query1, DataSource1 on Form1, set the related properties, so that the DBGRID1 can display the data in the table. Then, an event type onDrawColumnCell DBGrid1 in the following code, and then run the program procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i: integer; begin if gdSelected In State1 EXIT; // Valley change grid background color: if adoquery1.recno mod 2 = 0 THEN (Sender as tdbgrid) .canvas.brush.color: = clinfobk // Define background color else (sender as tdbgrid) .canvas .Brush.color: = RGB (191, 255, 223); // Define background color // Define Grid line Color: dbgrid1.defaultdrawColumnCell (Rect, Datacol, Column, State); with (sender as tdbgrid) .canvas Do // draws Cell Border Begin Pen.Color: = $ 00ff0000; // Define Brush Color (Blue) Moveto (Rect.right, Rect.Bottom); // Brush Positioning Lineto (Rect.right, Rect.Bottom) ; // Draw blue horizontal Pen.Color: = CLBTNFACE; / / Define Brush Color (Lan) Moveto (Rect.right, Rect.top); // Paint Position Lineto (Rect.right, Rect.Bottom) ; // Draw green end; end;
3. DBGRID specifies the DBComboBox on the column