Merhaba arkadaşlar bu makalemizde DBGrid nesnemizin sütun ve satırlarının font stillerini değiştireceğiz. Formumuza 1 adet DataSource ve ADOTable ekliyoruz. ADOTable nesnesinin ConnectionString kısmından Access veritabanımızı seçiyoruz.
Yine ADOTable nesnesinin TableName kısmından tablomuzu seçiyoruz. Bu örnekte tablomuzun ismi “Musteri” şeklindedir.
Şekil 1
unit dbgrid_column_font_style_Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, Vcl.Grids,
Vcl.DBGrids;
type
TMyDBGrid = class(TDBGrid);
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADOTable1: TADOTable;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
w,i:integer;
begin
//ornek olarak
//ilk sutunun genisligini ayarliyoruz
DBGrid1.Columns[0].Width:=50;
//ilk sutunun satirlarinin ortada olmasini sagliyoruz.
DBGrid1.Columns[0].Alignment:=taCenter;
for i :=0 to DBGrid1.DataSource.DataSet.RecordCount do
begin
//dbgrid deki satir yuksekligini ayarliyoruz.
if DataCol > 0 then begin
TMyDBGrid(DBGrid1).RowHeights[i]:=50;
end;
end;
//sutun basliklarinin font style ni ayarliyoruz.
for i := 0 to DBGrid1.Columns.Count-1 do
begin
DBGrid1.Columns[i].Title.Font.Size:=14;
DBGrid1.Columns[i].Title.Font.Color:=clGreen;
DBGrid1.Columns[i].Title.Font.Style:=[fsBold,fsItalic];
DBGrid1.Columns[i].Title.Alignment:=taCenter;
end;
//birinci satir font style ni ayarliyoruz.
TDBGrid(Sender).Canvas.Font.Name := 'Courier New';
TDBGrid(Sender).Canvas.Font.Size:=14;
DBGrid1.Columns[0].Font.Style := [fsItalic, fsBold];
DBGrid1.Columns[0].Font.Color := clBlue;
DBGrid1.Columns[0].Font.Size := 14;
//ikinci sutundaki satirdaki verilerin
//ilk harfini kirmizi yapiyoruz.
if DataCol = 1 then begin
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.Canvas.Font.Style := [fsBold];
DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2,
Column.Field.AsString[1]);
DBGrid1.Canvas.Font.Color := clWindowText;
w := DBGrid1.Canvas.TextWidth(Column.Field.AsString[1]);
DBGrid1.Canvas.Font.Style := [];
DBGrid1.Canvas.TextOut(Rect.Left + 2 + w,
Rect.Top + 2, Copy(Column.Field.AsString, 2, 255));
end;
//Soyadi sutununa gore satir rengini degistiriyoruz.
if (Column.Field.FieldName = 'Soyadi') then
begin
if Column.Field.AsString = 'Şahin' then
begin
DBGrid1.Canvas.Font.Color := clNavy;
DBGrid1.Canvas.Font.Style :=[fsBold];
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else
begin
DBGrid1.Canvas.Font.Color:= clMenuHighLight;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
//secilen satirin arka plan rengi degistiriliyor.
if (gdSelected in State) then
begin
DBGrid1.Canvas.Brush.Color := clLime;
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextOut(Rect.Left, Rect.Top,Column.Field.AsString);
end;
end;
end.
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder