DBGRID is one of the main means of displaying data in the Delphi language database programming. However, DBGRID default appearance has not seen monotonous and lack of ideas. In fact, we can fully achieve the purpose of beautifying DBGRID in our program. By programming, we can change DBGRID's head, grid, grid line foreground and background color, and the size and style of the related font. The following sample program demonstrates settings for DBGRID attributes, making the table displayed by Delphi as beautiful as the form in the web page. The operation of the sample program: Place the DBGRID1, Query1, DataSource1 on Form1, set the related properties, so that the DBGRID1 can display the data in the table. Then, type the following code in DBGRID1's OnDrawColumnCell event, and then run the program, you can see the magical result. This code is debugged in the Windows98, Delphi5.0 environment. procedure TMainForm.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i: integer; begin if gdSelected in State then Exit; // header defined font and background color : 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 (green) end; // Value 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 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.Left) , Rect.Bottom; // Brush positioning lineto (Rect.right, Rect.Bottom); // Draw blue horizontal line Pen.Color: = 0000FF00; // Define Brush Color (Green) MoveTo (Rect.right, Rect.top); // Brush Positioning Lineto (Rect.right, Rect.Bottom); // Draw Green Passer 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 specified list display DBComboBox