In Delphi In programming languages, DBGrid Is one of the main ways to display data. However DBGrid The default appearance is monotonous and lacks creativity. In fact, we canProgramProgram to beautify DBGrid The purpose of the appearance. Through programming, we can change DBGrid The header, grid, the foreground color and background color of the gridline, And the font size and style.
The following example demonstrates DBGrid Attribute settings to make Delphi The displayed table is as beautiful as the table on the webpage.
Run the sample program:
In Form1 Place on Dbgrid1 , Query1 , Performance1 Three database components, set relevant properties to make Dbgrid1 Displays table data. Then Dbgrid1 Of Ondrawcolumncell Type the following in the event:CodeAnd then run the program to see the magical results. This code is in Windows 98 , Delphi5.0 Environment debugging is successful.
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 font and background color of the header:
For I: = 0 to (sender as TDBGrid). Columns. Count-1 do
Begin
(Sender as TDBGrid). Columns [I]. Title. Font. Name: =' ';// 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;
// Change the grid background color on the line:
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 gridline color:
Dbgrid1.defadrawcolumncell (rect, datacol, column, State );
With (sender as TDBGrid). Canvas do // Image Cell Border
Begin
Pen. Color: = $00ff0000 ;// Define paint color ( Blue )
MoveTo (rect. Left, rect. Bottom );// Paint Brush Positioning
Lineto (rect. Right, rect. Bottom );// Draw a blue horizontal line
Pen. Color: = $0000ff00 ;// Define paint color ( Green )
MoveTo (rect. Right, rect. Top );// Paint Brush Positioning
Lineto (rect. Right, rect. Bottom );// Draw a green vertical line
End;
End;
2. delphi5-Change the barrierDBGridGrid color:
In Form1 Place on Dbgrid1 , Query1 , Performance1 Three database components, set relevant properties to make Dbgrid1 Displays table data. Then Dbgrid1 Of Ondrawcolumncell Type the following code in the event 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 State then exit ;// Change the grid background color on the line:
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 the gridline color:
Dbgrid1.defadrawcolumncell (rect, datacol, column, State );
With (sender as TDBGrid). Canvas do // Image Cell Border
Begin
Pen. Color: = $00ff0000 ;// Define paint color ( Blue )
MoveTo (rect. Left, rect. Bottom );// Paint Brush Positioning
Lineto (rect. Right, rect. Bottom );// Draw a blue horizontal line
Pen. Color: = clbtnface ;// Define paint color ( Blue )
MoveTo (rect. Right, rect. Top );// Paint Brush Positioning
Lineto (rect. Right, rect. Bottom );// Green
End;
End;
3. DBGridDisplay on specified ColumnsDbcombobox
Set Dbgrid1 Of Ondrawdatacell The event is as follows:
Procedure tform1.dbgrid1drawdatacell (Sender: tobject; const rect: trect; field: tfield; State: tgriddrawstate );
Begin
If (gdfocused in State) then
Begin
If (field. fieldname = dbcombobox1.datafield) then
Begin
Dbcombobox1.left: = rect. Left + dbgrid1.left;
Dbcombobox1.top: = rect. Top + dbgrid1.top;
Dbcombobox1.width: = rect. Right-rect. Left;
Dbcombobox1.height: = rect. Bottom-rect. Top;
Dbcombobox1.visible: = true;
End;
End;
End;
2) , DBGrid The specified cell is not displayed when no focus is obtained. Dbcombobox , Set Dbgrid1 Of Oncolexit The event is as follows:
Procedure tform1.dbgrid1colexit (Sender: tobject );
Begin
If dbgrid1.selectedfield. fieldname = dbcombobox1.datafield then
Begin
Dbcombobox1.visible: = false;
End;
End;
3) , When DBGrid When the specified column gets the focus Drawdatacell The event only draws cells and displays Dbcombobox , Dbcombobox No focus is obtained, and the data is still input in cells. In Dbgrid1 Of Keypress Call in event Sendmessage This Windows API Function transfers data input Dbcombobox To achieve Dbcombobox . Therefore, you must set Keypress The event is as follows:
Procedure tform1.dbgrid1keypress (Sender: tobject; var key: Char );
Begin
If (Key <> CHR (9) then
Begin
If (dbgrid1.selectedfield. fieldname = dbcombobox1.datafield) then
Begin
Dbcombobox1.setfocus;
Sendmessage (dbcombobox1.handle , Wm_char , Word (key) , 0 );
End;
End;
End;
trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 188955