本人是用stringgrid做的数据录入和显示。从主窗体调用这个窗体,在这个窗体上操作添加、删除、保存都没得问题。但是如果操作了这个窗体后关闭它(主窗体不关闭),再打开这个窗体就有问题了
1.如果开始没做什么操作就关闭再打开,stringgrid上面就没得数据显示
2.如果操作了添加、删除或保存,然后关闭此窗体(主窗体不关闭),再打开时就会出错:
adoquery:commandtext does not return a result set .然后就会向数据库插入一条先前操作了一记录。
真的搞不懂怎么回事,如果关闭全部,再打开操一次时数据能显示,也不会出错。只要是关闭子窗体再打开操作就会出问题。
还有一个问题是:我在程序里用了editkeydown事件后,在程序里的edit中就只能输入一个数字和字符,不要这个事件就不会出错。
下面是程序的代码:(还希望高手帮我看看)
unit Unit3;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons, ComCtrls, ToolWin, ExtCtrls;type
Tfrmactor = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Panel1: TPanel;
exitbutt: TBitBtn;
savebutt: TBitBtn;
delbutt: TBitBtn;
addbutt: TBitBtn;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure exitbuttClick(Sender: TObject);
procedure addbuttClick(Sender: TObject);
procedure savebuttClick(Sender: TObject);
procedure delbuttClick(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1Click(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
procedure moveedit; //移动edit1
procedure stringgridclear; //清空数据
procedure fullstringgrid; //显示数据
public
{ Public declarations }
end;var
frmactor: Tfrmactor;
implementation
uses unit4;
{$R *.dfm}
procedure Tfrmactor.FormClose(Sender: TObject; var Action: TCloseAction);
begin
frmactor.free;
end;
procedure tfrmactor.fullstringgrid;
begin
with dm.AQ_ACTOR do
begin
if dm.AQ_ACTOR.IsEmpty then exit;
while not eof do
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.Cells[0,stringgrid1.RowCount-1]:=fields.Fields[0].AsString;
stringgrid1.Cells[1,stringgrid1.RowCount-1]:=fields.Fields[1].AsString;
next;
end;
end;
end;
procedure tfrmactor.stringgridclear;
var i:integer;
begin
for i:=1 to stringgrid1.RowCount do
begin
stringgrid1.Cells[0,i]:='';
stringgrid1.Cells[1,i]:='';
end;
stringgrid1.RowCount:=1;
end;
procedure Tfrmactor.FormCreate(Sender: TObject);
begin
if dm.AQ_ACTOR.Active=false then
dm.AQ_ACTOR.Active:=true;
stringgridclear;
fullstringgrid;
edit1.Visible:=false;
end;procedure tfrmactor.moveedit;
var edit_rect:trect;
begin
edit_Rect:=stringgrid1.CellRect(stringgrid1.col,stringgrid1.row);
edit1.Left:=stringgrid1.left+edit_Rect.Left+2;
edit1.Top:=stringgrid1.top+edit_Rect.Top+4;
edit1.Height:=edit_rect.Bottom-edit_Rect.Top;
edit1.Width:=edit_rect.Right+2-edit_Rect.Left;
edit1.text:=stringgrid1.Cells[stringgrid1.col,stringgrid1.row];
edit1.Visible:=true;
edit1.SetFocus;
end;procedure Tfrmactor.exitbuttClick(Sender: TObject);
begin
frmactor.Free;
end;procedure Tfrmactor.addbuttClick(Sender: TObject);
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
end;procedure Tfrmactor.savebuttClick(Sender: TObject);
var i:integer;
begin
try
with dm.AQ_ACTOR do
begin
close;
sql.Clear;
sql.Add('delete from actor');
execsql;
close;
sql.Clear;
sql.Add('insert into actor');
sql.Add('(code,name)');
sql.Add('values(:code,:name)');
for i:=1 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells[0,i]='' then exit
else
begin
parameters.ParamByName('code').Value:=stringgrid1.Cells[0,i];
parameters.ParamByName('name').Value:=stringgrid1.Cells[1,i];
execsql;
end;
end;
messagedlg('保存成功',mtinformation,[mbok],0);
end;
except
messagedlg('程序问题,自己解决',mtinformation,[mbok],0);
end;
end;procedure Tfrmactor.delbuttClick(Sender: TObject);
var j:integer;
begin
if stringgrid1.RowCount=1 then exit;
for j:=stringgrid1.Row to stringgrid1.RowCount-1 do
begin
stringgrid1.Cells[0,j]:=stringgrid1.cells[0,j+1];
stringgrid1.Cells[1,j]:=stringgrid1.Cells[1,j+1];
end;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
edit1.Visible:=false;
end;procedure Tfrmactor.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
with stringgrid1 do
begincolcount:=2;
if arow=0 then
begin
canvas.Brush.Color:=clskyblue;
canvas.FillRect(rect);
if acol=0 then
begin
colwidths[0]:=120;
canvas.TextOut(rect.Left+10,rect.Top+5,'编码');
end;
if acol=1 then
begin
colwidths[1]:=190;
canvas.TextOut(rect.Left+10,rect.Top+5,'类型名称');
end;
canvas.Font.Color:=clHighlight;
canvas.Font.Charset:=GB2312_CHARSET;
end
else
begin
if (arow mod 2)=0 then
begin
canvas.Brush.Color:=clMoneyGreen;
canvas.FillRect(rect);
canvas.TextOut(rect.Left+5,rect.Top+5,cells[acol,arow]);
end
else
begin
canvas.Brush.Color:=clwhite;
canvas.FillRect(rect);
canvas.TextOut(rect.Left+5,rect.Top+5,cells[acol,arow]);
end;
end;
end;
end;
procedure Tfrmactor.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
if arow=0 then
begin
stringgrid1.Options:=stringgrid1.Options-[goediting];
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[goediting];
end;
end;
procedure Tfrmactor.StringGrid1Click(Sender: TObject);
begin
if stringgrid1.row=0 then
begin
stringgrid1.Options:=stringgrid1.Options-[goediting];
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[goediting];
moveedit;
end;end;procedure Tfrmactor.Edit1Exit(Sender: TObject);
begin
stringgrid1.Cells[stringgrid1.col,stringgrid1.row]:=edit1.Text;
edit1.Visible:=false;
end;procedure Tfrmactor.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=13) and (stringgrid1.Row<>0) then
begin
case stringgrid1.Col of
0:
begin
edit1exit(sender);
stringgrid1.Col:=stringgrid1.Col+1;
end;
1:
begin
edit1exit(sender);
if stringgrid1.Row<>stringgrid1.RowCount-1 then
begin
stringgrid1.Row:=stringgrid1.Row+1;
stringgrid1.Col:=0;
end
else
begin
stringgrid1.Rowcount:=stringgrid1.Rowcount+1;
stringgrid1.row:=stringgrid1.rowcount-1;
stringgrid1.Col:=0;
end;
end;
end;
end;
moveedit;
end;end.
1.如果开始没做什么操作就关闭再打开,stringgrid上面就没得数据显示
2.如果操作了添加、删除或保存,然后关闭此窗体(主窗体不关闭),再打开时就会出错:
adoquery:commandtext does not return a result set .然后就会向数据库插入一条先前操作了一记录。
真的搞不懂怎么回事,如果关闭全部,再打开操一次时数据能显示,也不会出错。只要是关闭子窗体再打开操作就会出问题。
还有一个问题是:我在程序里用了editkeydown事件后,在程序里的edit中就只能输入一个数字和字符,不要这个事件就不会出错。
下面是程序的代码:(还希望高手帮我看看)
unit Unit3;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Buttons, ComCtrls, ToolWin, ExtCtrls;type
Tfrmactor = class(TForm)
StringGrid1: TStringGrid;
Edit1: TEdit;
Panel1: TPanel;
exitbutt: TBitBtn;
savebutt: TBitBtn;
delbutt: TBitBtn;
addbutt: TBitBtn;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure exitbuttClick(Sender: TObject);
procedure addbuttClick(Sender: TObject);
procedure savebuttClick(Sender: TObject);
procedure delbuttClick(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure StringGrid1Click(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
procedure moveedit; //移动edit1
procedure stringgridclear; //清空数据
procedure fullstringgrid; //显示数据
public
{ Public declarations }
end;var
frmactor: Tfrmactor;
implementation
uses unit4;
{$R *.dfm}
procedure Tfrmactor.FormClose(Sender: TObject; var Action: TCloseAction);
begin
frmactor.free;
end;
procedure tfrmactor.fullstringgrid;
begin
with dm.AQ_ACTOR do
begin
if dm.AQ_ACTOR.IsEmpty then exit;
while not eof do
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.Cells[0,stringgrid1.RowCount-1]:=fields.Fields[0].AsString;
stringgrid1.Cells[1,stringgrid1.RowCount-1]:=fields.Fields[1].AsString;
next;
end;
end;
end;
procedure tfrmactor.stringgridclear;
var i:integer;
begin
for i:=1 to stringgrid1.RowCount do
begin
stringgrid1.Cells[0,i]:='';
stringgrid1.Cells[1,i]:='';
end;
stringgrid1.RowCount:=1;
end;
procedure Tfrmactor.FormCreate(Sender: TObject);
begin
if dm.AQ_ACTOR.Active=false then
dm.AQ_ACTOR.Active:=true;
stringgridclear;
fullstringgrid;
edit1.Visible:=false;
end;procedure tfrmactor.moveedit;
var edit_rect:trect;
begin
edit_Rect:=stringgrid1.CellRect(stringgrid1.col,stringgrid1.row);
edit1.Left:=stringgrid1.left+edit_Rect.Left+2;
edit1.Top:=stringgrid1.top+edit_Rect.Top+4;
edit1.Height:=edit_rect.Bottom-edit_Rect.Top;
edit1.Width:=edit_rect.Right+2-edit_Rect.Left;
edit1.text:=stringgrid1.Cells[stringgrid1.col,stringgrid1.row];
edit1.Visible:=true;
edit1.SetFocus;
end;procedure Tfrmactor.exitbuttClick(Sender: TObject);
begin
frmactor.Free;
end;procedure Tfrmactor.addbuttClick(Sender: TObject);
begin
stringgrid1.RowCount:=stringgrid1.RowCount+1;
end;procedure Tfrmactor.savebuttClick(Sender: TObject);
var i:integer;
begin
try
with dm.AQ_ACTOR do
begin
close;
sql.Clear;
sql.Add('delete from actor');
execsql;
close;
sql.Clear;
sql.Add('insert into actor');
sql.Add('(code,name)');
sql.Add('values(:code,:name)');
for i:=1 to stringgrid1.RowCount-1 do
begin
if stringgrid1.Cells[0,i]='' then exit
else
begin
parameters.ParamByName('code').Value:=stringgrid1.Cells[0,i];
parameters.ParamByName('name').Value:=stringgrid1.Cells[1,i];
execsql;
end;
end;
messagedlg('保存成功',mtinformation,[mbok],0);
end;
except
messagedlg('程序问题,自己解决',mtinformation,[mbok],0);
end;
end;procedure Tfrmactor.delbuttClick(Sender: TObject);
var j:integer;
begin
if stringgrid1.RowCount=1 then exit;
for j:=stringgrid1.Row to stringgrid1.RowCount-1 do
begin
stringgrid1.Cells[0,j]:=stringgrid1.cells[0,j+1];
stringgrid1.Cells[1,j]:=stringgrid1.Cells[1,j+1];
end;
stringgrid1.RowCount:=stringgrid1.RowCount-1;
edit1.Visible:=false;
end;procedure Tfrmactor.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
with stringgrid1 do
begincolcount:=2;
if arow=0 then
begin
canvas.Brush.Color:=clskyblue;
canvas.FillRect(rect);
if acol=0 then
begin
colwidths[0]:=120;
canvas.TextOut(rect.Left+10,rect.Top+5,'编码');
end;
if acol=1 then
begin
colwidths[1]:=190;
canvas.TextOut(rect.Left+10,rect.Top+5,'类型名称');
end;
canvas.Font.Color:=clHighlight;
canvas.Font.Charset:=GB2312_CHARSET;
end
else
begin
if (arow mod 2)=0 then
begin
canvas.Brush.Color:=clMoneyGreen;
canvas.FillRect(rect);
canvas.TextOut(rect.Left+5,rect.Top+5,cells[acol,arow]);
end
else
begin
canvas.Brush.Color:=clwhite;
canvas.FillRect(rect);
canvas.TextOut(rect.Left+5,rect.Top+5,cells[acol,arow]);
end;
end;
end;
end;
procedure Tfrmactor.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
if arow=0 then
begin
stringgrid1.Options:=stringgrid1.Options-[goediting];
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[goediting];
end;
end;
procedure Tfrmactor.StringGrid1Click(Sender: TObject);
begin
if stringgrid1.row=0 then
begin
stringgrid1.Options:=stringgrid1.Options-[goediting];
end
else
begin
stringgrid1.Options:=stringgrid1.Options+[goediting];
moveedit;
end;end;procedure Tfrmactor.Edit1Exit(Sender: TObject);
begin
stringgrid1.Cells[stringgrid1.col,stringgrid1.row]:=edit1.Text;
edit1.Visible:=false;
end;procedure Tfrmactor.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (key=13) and (stringgrid1.Row<>0) then
begin
case stringgrid1.Col of
0:
begin
edit1exit(sender);
stringgrid1.Col:=stringgrid1.Col+1;
end;
1:
begin
edit1exit(sender);
if stringgrid1.Row<>stringgrid1.RowCount-1 then
begin
stringgrid1.Row:=stringgrid1.Row+1;
stringgrid1.Col:=0;
end
else
begin
stringgrid1.Rowcount:=stringgrid1.Rowcount+1;
stringgrid1.row:=stringgrid1.rowcount-1;
stringgrid1.Col:=0;
end;
end;
end;
end;
moveedit;
end;end.
先感谢了!!!!!!!!!
begin
if dm.AQ_ACTOR.Active=false then
dm.AQ_ACTOR.Active:=true;
stringgridclear;
fullstringgrid;
edit1.Visible:=false;
end;
你在if dm.AQ_ACTOR.Active=false then
dm.AQ_ACTOR.Active:=true;
代码前把SQL语句重新写一下呢?可能执行的是关闭窗体前的SQL语句2。begin
edit1exit(sender);
if stringgrid1.Row<>stringgrid1.RowCount-1 then
begin
stringgrid1.Row:=stringgrid1.Row+1;
stringgrid1.Col:=0;
end
只要你不是点的STRINGGRID的最后一行,肯定是只执行一次的啊,edit1exit(sender);这句语句后,edit就看不到了
procedure Tfrmactor.FormCreate(Sender: TObject);
begin
if dm.AQ_ACTOR.Active=false then
dm.AQ_ACTOR.Active:=true;
就是这个原故
改成 with dm.aq_actor do
begin
close;
sql.clear;
sql.........
.....
end;