unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls, ExtCtrls, DBCtrls;

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    ADOTable1: TADOTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Label1: TLabel;
    InsertBtn: TButton;
    CancelBtn: TButton;
    DBNavigator1: TDBNavigator;
    procedure DataSource1DataChange(Sender: TObject; Field: TField);
    procedure DataSource1StateChange(Sender: TObject);
    procedure InsertBtnClick(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    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.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if(Field <> Nil ) then
    showmessage( Field.DisplayName + ' changed to ' +
  Field.Asstring);
end;

procedure TForm1.DataSource1StateChange(Sender: TObject);
var
  S : String;
begin
  Case ADOTable1.State of
    dsInactive : S := 'Inactive';
    dsBrowse : S := 'Browse';
    dsEdit : S := 'Edit';
    dsInsert : S := 'SetKey';
    dsSetKey : S := 'SetKey';
  end;
  Label1.Caption := S;
  InsertBtn.Enabled := (ADOTable1.State = dsBrowse);
  CancelBtn.Enabled := ADOTable1.State in [dsInsert,dsEdit,dsSetKey]
end;


procedure TForm1.InsertBtnClick(Sender: TObject);
var
  I : Integer;
begin
{  for I := 0 to DBGrid1.Columns.Count - 1 do
    ShowMessage( DBGrid1.Fields[I].AsString );}
DBNavigator1.VisibleButtons:=DBNavigator1.VisibleButtons-[nbDelete];
end;


procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  if DBGrid1.SelectedField <> nil then
    showmessage(DBGrid1.SelectedField.AsString);
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i :integer;
begin
  if gdSelected in State then Exit;
//定义表头的字体和背景颜色：
    for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
    begin
      (Sender as TDBGrid).Columns[i].Title.Font.Name :='宋体'; //字体
      (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小
      (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色)
      (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色)
    end;
//隔行改变网格背景色：
  if ADOTable1.RecNo mod 2 = 0 then
    (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色
  else
    (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色
//定义网格线的颜色：
    DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  with (Sender as TDBGrid).Canvas do //画 cell 的边框
  begin
    Pen.Color := $00ff0000; //定义画笔颜色(蓝色)
    MoveTo(Rect.Left, Rect.Bottom); //画笔定位
    LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线
    Pen.Color := $0000ff00; //定义画笔颜色(绿色)
    MoveTo(Rect.Right, Rect.Top); //画笔定位
    LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线
  end;
end;


end.