About DBGRID Classification Color Display

xiaoxiao2021-03-06  48

Turn in 9CBS two days, find that many people ask questions about the classification of DBGRID or DBGRIDEH, today is idle, talk about this question. In fact, for DBGRID's self-drawn, Delphi provides two events for us to use, one is drawing: onDrawdatacell (sender: TObject; const: TRECT; Field: Tfield; State: TgridDrawState); The other is the color: OnDrawColumncell (Sender: Tobject; Const Rect: TRECT; DATACOL: Integer; Column: Tcolumn; State: TgriddrawState; this is a great convenience. For DBGRID, the classification color is displayed, it should be apparent to the first event. So how can I be classified according to the color, I actually sound hard, but it is actually very simple. First, a type of TCOLOR is defined, and then in the instance of the data set, the length of the array is defined as the number of records of the data set. The color value of each element of the array is then calculated according to your classification, and each element of the array should correspond to a record of the data set. Finally, write the following code in your OnDrawColumnCell event. DBGRID1.CANVAS.BRUSH.COLOR: = C [Low (c) dbgrid1.datasource.dataset.recno () - 1]; The above is the background color of the set row, if you want to set the font color, you only need to use dbgrid1.canvas. Font.color: = C [Low (c) dbgrid1.datasource.DataSet.Recno () - 1]; Finally don't forget to add a dbgrid1.defaultdrawDatacell (Rect, Field, State); so you can want to think Shows the color you like. The above is just my little fool, some people will say, you do this, you need to pass the database all over it like this. Yes, you have to show that you must traverse the database, but for large databases, you can do not necessarily in the database's afteropen, you can only traverse the database one by one, for example, you are doing something else. Events such as the CLIENTDATASET's AFTERGETRECORDS. The other best method is to calculate the value of this array with a stored procedure, which is the most efficient method. Of course, these are principles, and this is not discussed in detail here. Below is a simple DEMO to display DBGRID's data per 10 sets of red green two colors in Delphi7.0 to pass. Unit unit1;

Interface

Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Grids, DBGRIDS, DB, DBTABLES

type TForm1 = class (TForm) DataSource1: TDataSource; Query1: TQuery; DBGrid1: TDBGrid; procedure DBGrid1DrawDataCell (Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); procedure Query1AfterOpen (DataSet: TDataSet); procedure FormCreate (Sender: TOBJECT); private {private declarations} end; var form1: tform1; cliff: array of tcolor; importation {$ r * .dfm}

procedure TForm1.FormCreate (Sender: TObject); begin Query1.Active: = False; Query1.DatabaseName: = 'DBDEMOS'; Query1.SQL.Add ( 'select * from orders'); DataSource1.DataSet: = Query1; Dbgrid1. DataSource: = DataSource1; dbgrid1.align: = alclient; query1.active: = true;

Procedure TForm1.Query1AFterOpen; Var i, aa: integer; c: tcolor; begin query1.disablecontrols; setlength (cliff, query1.record); query1.first; i: = low (clef); CLF [i ]: = CLRED; C: = CLRED; AA: = 1; While Not Query1.eof Do Begin IF (Query1.Recno - AA)> 9 The begin AA: = AA 10; if c = CLRED THEN C: = CLGreen Else C: = CLRED; END; CLF [I]: = C; INC (I); query1.next; end; query1.first; query1.enablecontrols;

procedure TForm1.DBGrid1DrawDataCell (Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin DBGrid1.Canvas.Brush.Color: = clf [Dbgrid1.DataSource.DataSet.RecNo-1 Low (clf)] DBGRID1.DEFAULTDRAWDATACELL (Rect, Field, State); End;

End.

转载请注明原文地址:https://www.9cbs.com/read-79943.html

New Post(0)