只要在DrawDataCell事件里面处理就好了,主要是调整控件的位置,这里有一个别人的例子,在DBGrid中嵌入了一个DBComboBox
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, Grids, DBGrids, DB, DBTables;type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBComboBox1: TDBComboBox;
DBComboBox2: TDBComboBox;
DBComboBox3: TDBComboBox;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Field.FieldName = DBCombobox1.DataField) then
begin
DBCombobox1.Left :=Rect.Left + DBgrid1.Left;
DBCombobox1.Top := Rect.Top + DBgrid1.Top;
DBCombobox1.Width := Rect.Right - Rect.Left;
DBCombobox1.Visible :=True;
end;
if (Field.FieldName = DBCombobox2.DataField) then
begin
DBCombobox2.Left :=Rect.Left + DBgrid1.Left;
DBCombobox2.Top := Rect.Top + DBgrid1.Top;
DBCombobox2.Width := Rect.Right - Rect.Left;
DBCombobox2.Visible :=True;
end; if (Field.FieldName = DBCombobox3.DataField) then
begin
DBCombobox3.Left :=Rect.Left + DBgrid1.Left;
DBCombobox3.Top := Rect.Top + DBgrid1.Top;
DBCombobox3.Width := Rect.Right - Rect.Left;
DBCombobox3.Visible :=True;
end;
end;
end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBCombobox1.DataField then
begin
DBCombobox1.Visible := false;
end;
if DBGrid1.SelectedField.FieldName = DBCombobox2.DataField then
begin
DBCombobox2.Visible := false;
end;
if DBGrid1.SelectedField.FieldName = DBCombobox3.DataField then
begin
DBCombobox3.Visible := false;
end;
end;procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName = DBCombobox1.DataField ) then
begin
DBCombobox1.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end; if (DBGrid1.SelectedField.FieldName = DBCombobox2.DataField ) then
begin
DBCombobox2.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end; if (DBGrid1.SelectedField.FieldName = DBCombobox3.DataField ) then
begin
DBCombobox3.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end;
end;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, Grids, DBGrids, DB, DBTables;type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBComboBox1: TDBComboBox;
DBComboBox2: TDBComboBox;
DBComboBox3: TDBComboBox;
procedure DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Field.FieldName = DBCombobox1.DataField) then
begin
DBCombobox1.Left :=Rect.Left + DBgrid1.Left;
DBCombobox1.Top := Rect.Top + DBgrid1.Top;
DBCombobox1.Width := Rect.Right - Rect.Left;
DBCombobox1.Visible :=True;
end;
if (Field.FieldName = DBCombobox2.DataField) then
begin
DBCombobox2.Left :=Rect.Left + DBgrid1.Left;
DBCombobox2.Top := Rect.Top + DBgrid1.Top;
DBCombobox2.Width := Rect.Right - Rect.Left;
DBCombobox2.Visible :=True;
end; if (Field.FieldName = DBCombobox3.DataField) then
begin
DBCombobox3.Left :=Rect.Left + DBgrid1.Left;
DBCombobox3.Top := Rect.Top + DBgrid1.Top;
DBCombobox3.Width := Rect.Right - Rect.Left;
DBCombobox3.Visible :=True;
end;
end;
end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBCombobox1.DataField then
begin
DBCombobox1.Visible := false;
end;
if DBGrid1.SelectedField.FieldName = DBCombobox2.DataField then
begin
DBCombobox2.Visible := false;
end;
if DBGrid1.SelectedField.FieldName = DBCombobox3.DataField then
begin
DBCombobox3.Visible := false;
end;
end;procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName = DBCombobox1.DataField ) then
begin
DBCombobox1.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end; if (DBGrid1.SelectedField.FieldName = DBCombobox2.DataField ) then
begin
DBCombobox2.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end; if (DBGrid1.SelectedField.FieldName = DBCombobox3.DataField ) then
begin
DBCombobox3.SetFocus;
SendMessage(DBCombobox1.Handle,WM_CHAR,word(Key),0);
end;
end;
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货