我想在dbgrid中显示下拉菜单,通过在csdn中找到了下面的代码;但加好以后出现了下列错误:当我下拉combobox2选择好了后,dbgrid中相应单元格没有当时就显示数值,移走后显示的是原来的数据,保存后再刷新才显示更改的数据,而且再保存时它弹出错误说:dataset not in edit or insert mode;可是我已经按下了DBNavigator1的nbedit了,为什么
在窗体上放一个DBComboBox,Visible为False;
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.
在窗体上放一个DBComboBox,Visible为False;
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.
ding,大家帮我顶呀:(