很简单,将欲嵌入网格的控件的Visible设置为False,然后设置网格的以下三个事件
就能实现:
1、DBGrid.DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
2、DBGrid.ColExit(Sender: TObject);
3、DBGrid.KeyPress(Sender: TObject; var Key: Char);
举一个例子,将一个Boolean型字段在网格中编辑(状态)时用检查框(DBCheckBox)实现,
并在网格显示(状态)时用相应位图(ImageTrue,ImageFalse:TImage)来代替。
//Draw cell in grid
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Field.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 1;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 1;
DBCheckBox1.Width := Rect.Right - Rect.Left{ - 1};
DBCheckBox1.Height := Rect.Bottom - Rect.Top{ - 1};
DBCheckBox1.Visible := True;
end
else {in this else area draw any stay behind bit maps}
begin
if (Field.FieldName = DBCheckBox1.DataField) then
begin
if TableGridDataCheckBox.AsBoolean then
DBGrid1.Canvas.Draw(Rect.Left,Rect.Top, ImageTrue.Picture.Bitmap)
else
DBGrid1.Canvas.Draw(Rect.Left,Rect.Top, ImageFalse.Picture.Bitmap)
end
end;
//when jump away cell, do it
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := false;
end;
//Key Press in grid
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end;
end;
end;
我想woll2woll的东东的实现大概也是如此。