procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if gdSelected in State then
begin
Canvas.Font.Color :=clBackground;
Canvas.FillRect(Rect);
end;
DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i : integer;
begin
if column.field.AsString=ID then
DBGrid1.Canvas.Brush.color:=clyellow
else
DBGrid1.Canvas.Brush.color:=clred;
for i:=0 to dbgrid1.Columns.Count-1 do
dbgrid1.DefaultDrawColumnCell(rect,i,column,state);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with dbgrid1 do
begin
if datasource<>nil then
if datasource.dataset<>nil then
if datasource.dataset.active then
if datasource.dataset.Eof then
datasource.dataset.first
else
datasource.dataset.next;
end;
names:=dbgrid1.datasource.dataset.fieldbyname('name').asstring;
DBGrid1.Repaint ;
end;
把自动DBGRID滚动的代码改成这样,作为选中行的背景色当然会改变
procedure TForm1.Timer1Timer(Sender: TObject);
begin
with dbgrid1 do
begin
if datasource<>nil then
if datasource.dataset<>nil then
if datasource.dataset.active then
if datasource.dataset.Eof then
datasource.dataset.first
else
datasource.dataset.next;
end;
end; end;