DBGrid row and column color settings in Delphi

Source: Internet
Author: User
1. Vertical zebra effect: the odd series and even columns of the grid are displayed in different colors to distinguish adjacent data columns.

File: // write the following code in the DrawColumnCell event of DbGrid:

Case DataCol Mod 2 = 0

True: DbGrid1.Canvas. Brush. Color: = clBlue; file: // blue for even Columns

False: DbGrid1.Canvas. Brush. Color: = clAqua; file: // light green for odd series

End;

DbGrid1.Canvas. Pen. Mode: = pmMask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );


2. Vertical zebra crossings, and the current cell effect highlighted in red: to highlight the selected fields.
File: // modify the above Code:

Case DataCol Mod 2 = 0

True: DbGrid1.Canvas. Brush. Color: = clBlue; file: // blue for even Columns

False: DbGrid1.Canvas. Brush. Color: = clAqua; file: // light green for odd series

End;

If (State = [gdSelected]) or (State = [gdSelected, gdFocused]) then

If Not DbGrid1.SelectedRows. CurrentRowSelected then

DbGrid1.Canvas. Brush. Color: = clRed; file: // red DbGrid1.Canvas. Pen. Mode: = pmMask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );

The above two methods highlight the Column Display Effect.

3. Highlight the selected row in red in the data grid.

Set the dgRowSelect attribute in the Options attribute of the DbGrid control to true, and the Color attribute to clAqua (background Color). Write the following code in the DbGrid DrawColumnCell event:

If (State = [gdSelected]) or (State = [gdSelected, gdFocused]) then

DbGrid1.Canvas. Brush. color: = clRed; file: // The current row is displayed in red. Other rows use the light green background.

DbGrid1.Canvas. pen. mode: = pmmask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );

4. Zebra Effect of line highlighting: not only highlight the current row, but also distinguish different columns (fields ).

File: // other attributes are set to the same as 3. Modify the above Code:

If (State = [gdSelected]) or (State = [gdSelected, gdFocused]) then

Begin

Case DataCol Mod 2 = 0

True: DbGrid1.Canvas. Brush. color: = clRed; file: // red is displayed for the even column of the currently selected row.

False: DbGrid1.Canvas. Brush. color: = clblue; file: // the odd sequence of the currently selected row is blue.

End;

DbGrid1.Canvas. pen. mode: = pmmask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );

End;



5. Horizontal zebra crossings, highlighted in red at the same time.
File: // other attributes are set to the same as 3. Modify the above Code:

Case Table1.RecNo mod 2 = 0 of file: // judge based on the record number of the dataset

True: DbGrid1.Canvas. Brush. color: = clAqua; file: // displays even rows in light green.

False: DbGrid1.Canvas. Brush. color: = clblue; file: // the odd number of rows in blue.

End;

If (State = [gdSelected]) or (State = [gdSelected, gdFocused]) then file: // display the selected row in red

DbGrid1.Canvas. Brush. color: = clRed;

DbGrid1.Canvas. pen. mode: = pmMask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );

6. Bidirectional zebra effect: select different colors for the selected lines and use vertical zebra effect to distinguish different columns.

File: // other attributes are set to the same as 3. Modify the above Code:

Case Table1.RecNo mod 2 = 0 of file: // judge based on the record number of the dataset

True: DbGrid1.Canvas. Brush. color: = clAqua; file: // displays even rows in light green.

False: DbGrid1.Canvas. Brush. color: = clblue; file: // the odd number of rows in blue.

End;

If (State = [gdSelected]) or (State = [gdSelected, gdFocused]) then

Case DataCol mod 2 = 0

True: DbGrid1.Canvas. Brush. color: = clRed; file: // The even column of the currently selected row is in red.

False: DbGrid1.Canvas. Brush. color: = clGreen; file: // the odd sequence of the currently selected row in green.

End;

DbGrid1.Canvas. pen. mode: = pmMask;

Dbgrid1.defadrawcolumncell (Rect, DataCol, Column, State );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.