unit testDBG;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls,Types;
type
TForm1 = class(TForm)
DBG: TDBGrid;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
DataSource1: TDataSource;
procedure DBGDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGKeyPress(Sender: TObject; var Key: Char);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
MyRect=array[0..200] of Tpoint;
var
ME:MyRect;//記錄已經修改過記錄的單元格的左上叫的點
i:integer;
Cpoint:Tpoint;//當前單元格左上角的點
procedure TForm1.DBGDrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
J:integer;
begin
if (gdFocused in State) {or (gdSelected in State) or (gdFixed in state)} then
begin
{下面的語句是記錄格當前活動單元格的左上角坐標}
Cpoint.X:=rect.Left;
cpoint.Y:=rect.Top;
end;
for J:=0 to I do
begin
if (rect.Top=me[j].Y) and (rect.left=me[j].X) then
begin//判斷該單元格是否存在於已修改的數組的記錄中!
dbg.Canvas.Brush.Color:=clRed;
dbg.DefaultDrawColumnCell(rect,Datacol,column,state);
end;
end;
end;
procedure TForm1.DBGKeyPress(Sender: TObject; var Key: Char);
{由於沒有找到更好的記錄點,所以如果修改的時候只按了一個按鍵的話將不會觸發這個語句!
因為第一次執行這個語句的時候數據還沒有處於修改或者插入狀態,本人只測試了修改狀態,添加狀態沒測試過!}
var
J:integer;
begin
if dbg.DataSource.DataSet.State in [dsEdit, dsInsert] then
begin//處於就該狀態:
for J:=0 to i do
begin//判斷該格是否已經記錄了
if (me[j].X=cpoint.X) and (me[j].Y=cpoint.Y) then
exit;
end;//如果沒記錄就把它記錄下來!
me[i].X:=cpoint.X;
me[i].Y:=cpoint.Y;
i:=i+1;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
I:=0;//初始化數組起點
end;
end.
本人只是做了修改的測試,是成功的,這麼少的代碼肯定是不能符合大家的更高的需求,這裡只是完成了最基本的功能,方便大家理解,有什麼更高的要求當然是根據各人的需要,玩一些數字游戲罷了!大家可以把它修改成更安全,更好用的東西!
謝謝大家的捧場!