unit test;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, ADODB;type TFrmTest = class(TForm) myADOConnection: TADOConnection; myADOQuery: TADOQuery; myDataSource: TDataSource; DBGrid1: TDBGrid; ButtonSave: TButton; ButtonCancel: TButton; procedure FormActivate(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure ButtonSaveClick(Sender: TObject); procedure ButtonCancelClick(Sender: TObject); procedure DBGrid1KeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end;var FrmTest: TFrmTest;implementation//uses DataModuleUnit;//var// lCanEdit: Boolean;{$R *.dfm}procedure TFrmTest.FormActivate(Sender: TObject);var i: Integer; Str1, StrUserName, StrPassword, StrTableName: String; ServerName, DatabaseName: String; s: String;begin ServerName := '.'; DatabaseName := 'eway2000'; StrTableName := 'TB_Area'; StrUserName := 'sa'; StrPassword := ''; Str1 := ''; Str1 := Str1 + 'Provider=' + 'SQLOLEDB.1' + ';'; Str1 := Str1 + 'Persist Security Info=' + 'False' + ';'; Str1 := Str1 + 'User ID=' + StrUserName + ';'; Str1 := Str1 + 'Password=' + StrPassword + ';'; Str1 := Str1 + 'Initial Catalog=' + '"' + DataBaseName + '"' + ';'; Str1 := Str1 + 'Data Source=' + '"' + ServerName +'"'; with myADOConnection do begin try Close; LoginPrompt := False; ConnectionString := Str1; Open; except ServerName := ''; ShowMessage('连接指定数据库' + DatabaseName + '失败!'); Exit; end; end; myADOQuery.LockType := ltBatchOptimistic; with myADOQuery do begin Connection := myADOConnection; Close; SQL.Clear; SQL.Add('select * from ' + '"' + StrTableName + '"'); Open; end; myDataSource.DataSet := myADOQuery;// myDataSource.AutoEdit := False; DBGrid1.DataSource := myDataSource; case myADOQuery.LockType of ltUnspecified: s := 'ltUnspecified'; ltReadOnly: s := 'ltReadOnly'; ltPessimistic: s := 'ltPessimistic'; ltOptimistic: s := 'ltOptimistic'; ltBatchOptimistic: s := 'ltBatchOptimistic'; else s := 'Nothing?'; end; ShowMessage('ADOQuery.LockType: ' + s); for i := 0 to DBGrid1.Columns.Count -1 do //设置标题居中 begin DBGrid1.Columns.Title.Alignment := taCenter; end;// DBGrid1.Columns[0].Color := clYellow; DBGrid1.ReadOnly := False; FrmTest.KeyPreview := True;end;procedure TFrmTest.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin if MessageBox(Self.Handle,'您真的要退出程序吗?','退出提示', MB_OKCANCEL+MB_DEFBUTTON2+MB_ICONEXCLAMATION)=idOk then CanClose := True else CanClose := Falseend;procedure TFrmTest.ButtonSaveClick(Sender: TObject);begin if myADOQuery <> nil then myADOQuery.CancelUpdates;end;procedure TFrmTest.ButtonCancelClick(Sender: TObject);begin if myADOQuery <> nil then myADOQuery.UpdateBatch;end;procedure TFrmTest.DBGrid1KeyPress(Sender: TObject; var Key: Char);begin if Key = #13 then //#13: 回车键; 如果是回车键,模拟按Tab键; begin Key := #0; Keybd_Event(VK_TAB, MapvirtualKey(VK_TAB, 0), 0, 0); end;end;//initialization// lCanEdit := False;//finalizationend.