DBGridの使用方法(2)

 

値によって文字の色を変える
1行おきに行の色を変える
選択した行の特定のフィールド値を取得する
クリックした項目の値を取得する
値によって文字の色を変える 1/2
1/2
値によって文字の色を変えるときはDBGridコンポーネントのOnDrawColumnCellイベントでコードを記述します。
各セルの売上金額値の判断には、DrawColumnCellのパラメータ「Column」を利用します。DBGridのCanvas.Font.Colorプロパティで文字色を設定し、DefaultDrawColumnCellメソッドで描画します。

DBGridコンポーネントのOnDrawColumnCellイベントでコードを次のように記述します。




procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
//売上がマイナスなら赤く表示
if (Column.FieldName = 'URIAGE') and
(Column.Field.AsCurrency < 0) then
DBGrid1.Canvas.Font.Color := clRed; //描画
DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);

end;

 
1/2