用dbcombobox1 为例:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdfocused in state) then
begin
DBCombobox1.Left := Rect.Left + DBGrid1.Left;
DBCombobox1.Top := Rect.Top + DBGrid1.top;
DBCombobox1.Width := Rect.Right - Rect.Left;
DBCombobox1.Height := Rect.Bottom - Rect.Top;
DBCombobox1.Visible := True;
end;
end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdfocused in state) then
begin
DBCombobox1.Left := Rect.Left + DBGrid1.Left;
DBCombobox1.Top := Rect.Top + DBGrid1.top;
DBCombobox1.Width := Rect.Right - Rect.Left;
DBCombobox1.Height := Rect.Bottom - Rect.Top;
DBCombobox1.Visible := True;
end;
end;
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if gdSelected in state then
SetBkColor(dbgrid1.canvas.handle,clOlive)
else
setbkcolor(dbgrid1.canvas.handle,clwhite);
dbgrid1.Canvas.TextRect(rect,0,0,field.AsString);
dbgrid1.Canvas.Textout(rect.Left,rect.Top,field.AsString)
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
oldcolor:tcolor;
oldpm:tpenmode ;
begin
if table1.Fields[0].AsString='34' then
begin
oldpm:= dbGrid1.Canvas.pen.mode;
oldcolor:=dbGrid1.Canvas.Brush.color;
dbGrid1.Canvas.Brush.color:=clblue;
dbgrid1.Canvas.pen.mode:=pmmask;
dbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
dbGrid1.Canvas.Brush.color:=oldcolor;
dbGrid1.Canvas.pen.mode:=oldpm;
end;
end;