03 Mei 2009

Mewarnai Baris DbGrid

// To set the color of a row in a DBGrid, use the following component

Unit uDBSDBGrid;

interface

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DB,
Grids, DBGrids;

Type
TDBSDBGrid = Class(TDBGrid)
private
FRowColorEven : TColor;
FRowColorOdd : TColor;

procedure SetRowColorEven(Const Value: TColor);
procedure SetRowColorOdd(Const Value: TColor);
protected
procedure DrawColumnCell(Const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState); override;
public
constructor Create(aOwner: TComponent); override;
function AddColumn(Const aFieldName: String;
Const aMaxValue: Double = 0; aFormat: String = ''): TColumn;
published
property RowColorEven : TColor read FRowColorEven write SetRowColorEven;
property RowColorOdd : TColor read FRowColorOdd write SetRowColorOdd;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('DBS', [TDBSDBGrid]);
end;

{ TDBSDBGrid }

function TDBSDBGrid.AddColumn(Const aFieldName: String;
Const aMaxValue: Double = 0; aFormat: String = ''): TColumn;
begin
Result := Columns.Add;
Result.FieldName := aFieldName;
If (DataSource.DataSet.FieldByName(aFieldName) is TNumericField) Then
begin
If (aFormat = '') Then
aFormat := (DataSource.DataSet.FieldByName(aFieldName) as TNumericField).DisplayFormat
Else
(DataSource.DataSet.FieldByName(aFieldName) as TNumericField).DisplayFormat := aFormat;
If (aMaxValue <> 0) Then
Result.Width := Canvas.TextWidth(FormatFloat(aFormat, aMaxValue)) + 4;
end;
end;

constructor TDBSDBGrid.Create(aOwner: TComponent);
begin
inherited;
//---
FRowColorEven := clWindow;
FRowColorOdd := clInfoBk;
end;

procedure TDBSDBGrid.DrawColumnCell(Const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState);
begin
inherited;
//---
If (gdSelected in State) And
(Screen.ActiveControl = Self) And
(Not (csDesigning in ComponentState)) Then
//--- nothing
Else
If (Color <> Column.Color) Then
//--- Column has own defined color
Else
begin
If odd(DataSource.DataSet.RecNo) Then
Canvas.Brush.Color := FRowColorOdd
Else
Canvas.Brush.Color := FRowColorEven;
DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;

procedure TDBSDBGrid.SetRowColorEven(Const Value: TColor);
begin
FRowColorEven := Value;
Repaint;
end;

procedure TDBSDBGrid.SetRowColorOdd(Const Value: TColor);
begin
FRowColorOdd := Value;
Repaint;
end;

end.

Tidak ada komentar:

Posting Komentar