unit MasterDBForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  BaseForm, AnyPrint, AnyGrids, ComCtrls, Buttons, ExtCtrls, Db, ADODB,
  DBCtrls;

type
  TfrmMasterDB = class(TfrmBase)
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    DBNavigator1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure AnyGridSetText(Sender: TObject; Row, Col: Integer;
      var Text: WideString);
    procedure AnyGridGetText(Sender: TObject; Row, Col: Integer;
      var Text: WideString);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMasterDB: TfrmMasterDB;

implementation

{$R *.DFM}

procedure TfrmMasterDB.FormCreate(Sender: TObject);
begin
  inherited;
  ADOQuery1.ConnectionString:=Format('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sdb.mdb;Persist Security Info=False',[ExtractFilePath(Application.ExeName)]);
  ADOQuery1.Open;  
end;

procedure TfrmMasterDB.AnyGridSetText(Sender: TObject; Row, Col: Integer;
  var Text: WideString);
var
  CellName:string;
begin
  if AnyGrid.PeekCell(Row,Col) then
  begin
    CellName:=AnyGrid.Cells(Row,Col).Name;
  end
  else
    CellName:='';
  if SameText(CellName,'Gender') or SameText(CellName,'Department') then
    Text:=IntToStr(AnyGrid.Cells(Row,Col).Items.IndexOf(Text));
end;

procedure TfrmMasterDB.AnyGridGetText(Sender: TObject; Row, Col: Integer;
  var Text: WideString);
var
  CellName:string;
begin                                      
  if AnyGrid.PeekCell(Row,Col) then
  begin
    CellName:=AnyGrid.Cells(Row,Col).Name;
  end
  else
    CellName:='';
  if SameText(CellName,'Gender') or SameText(CellName,'Department') then
    Text:=AnyGrid.Cells(Row,Col).Items[StrToIntDef(Text,0)];
end;

procedure TfrmMasterDB.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if ADOQuery1.Modified and (ADOQuery1.State in [ dsEdit, dsInsert]) then
    case Application.MessageBox('是否要保存所做的修改？','提示',MB_ICONINFORMATION+MB_YESNOCANCEL) of
      IDYES: ADOQuery1.Post;
      IDCANCEL:begin
        Action:=caNone;
        Exit;
      end;
    end;
  inherited;
end;

end.
