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.
本人只是做了修改的测试,是成功的,这么少的代码肯定是不能符合大家的更高的需求,这里只是完成了最基本的功能,方便大家理解,有什么更高的要求当然是根据各人的需要,玩一些数字游戏罢了!大家可以把它修改成更安全,更好用的东西!
谢谢大家的捧场!
本文地址:http://com.8s8s.com/it/it5075.htm