stringgrid 怎么刷新?
比如FORM1有一stringgrid,2BUTTON(添加和删除),点添加跳出一窗口,假设都是EDIT吧,将EDIT的值都传入SQL里,然后关闭这个窗口,
我想让这个STRINGGRID控件自己刷新,比如我现在加入了第一次数据,它显示了一行,加入2行,显示2行 不要退出来再进入才会刷新该怎么实现?
procedure TForm7.Button1Click(Sender: TObject);
var
a,c,b,d,e: string;
f,g: Integer;
begin
a := Edit1.Text;
b := Edit3.Text;
c := Edit5.Text;
d := Edit7.Text;
e := edit2.Text;
with DM do
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into ptype(typeid,Fullname,barcode,Standard,area)values(' + a + ' , ''' + b + ''' , ' + c + ' , ''' + d + ''' , ''' + e + ''')');
ADOQuery1.ExecSQL;
end; with Form5 do
begin
DM.ADOQuery1.Close;
DM.ADOQuery1.SQL.Clear;
DM.ADOQuery1.SQL.Add('select * from ptype');
DM.ADOQuery1.Open; DM.ADOQuery1.First;
StringGrid1.RowCount := DM.ADOQuery1.RecordCount + 1;
StringGrid1.ColCount := DM.ADOQuery1.FieldCount + 1;
for F :=0 to DM.ADOQuery1.RecordCount-1 do
begin
for g := 0 to DM.ADOQuery1.FieldCount -1 do
begin
StringGrid1.Cells[f+1,g+1] := DM.ADOQuery1.Fields[g].AsString;
end;
DM.ADOQuery1.Next;
end; end;
执行后是会添加,但无法刷新
比如FORM1有一stringgrid,2BUTTON(添加和删除),点添加跳出一窗口,假设都是EDIT吧,将EDIT的值都传入SQL里,然后关闭这个窗口,
我想让这个STRINGGRID控件自己刷新,比如我现在加入了第一次数据,它显示了一行,加入2行,显示2行 不要退出来再进入才会刷新该怎么实现?
procedure TForm7.Button1Click(Sender: TObject);
var
a,c,b,d,e: string;
f,g: Integer;
begin
a := Edit1.Text;
b := Edit3.Text;
c := Edit5.Text;
d := Edit7.Text;
e := edit2.Text;
with DM do
begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('insert into ptype(typeid,Fullname,barcode,Standard,area)values(' + a + ' , ''' + b + ''' , ' + c + ' , ''' + d + ''' , ''' + e + ''')');
ADOQuery1.ExecSQL;
end; with Form5 do
begin
DM.ADOQuery1.Close;
DM.ADOQuery1.SQL.Clear;
DM.ADOQuery1.SQL.Add('select * from ptype');
DM.ADOQuery1.Open; DM.ADOQuery1.First;
StringGrid1.RowCount := DM.ADOQuery1.RecordCount + 1;
StringGrid1.ColCount := DM.ADOQuery1.FieldCount + 1;
for F :=0 to DM.ADOQuery1.RecordCount-1 do
begin
for g := 0 to DM.ADOQuery1.FieldCount -1 do
begin
StringGrid1.Cells[f+1,g+1] := DM.ADOQuery1.Fields[g].AsString;
end;
DM.ADOQuery1.Next;
end; end;
执行后是会添加,但无法刷新
在form2中增加一条后,调用form1中的这个过程,我是用发条消息给form1让form1来执行刷新。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Grids, DB, ADODB;
const WM_LOCATION = WM_USER + 8001; //刷新type
TForm1 = class(TForm)
Button1: TButton;
StrGrid: TStringGrid;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure setstrgrid(strGrid:TStringGrid); //初始化stringgrid
procedure SetGridInfo(StrGrid:TStringGrid;sOraQuery:TADOQuery); //从数据集加载数据到stringgrid
procedure RefreshStrGrid; //刷新的过程
Procedure RefreshInfo(Var Msg:TMessage); Message WM_LOCATION;//消息
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}{ TForm1 }
procedure TForm1.setstrgrid(strGrid:TStringGrid);
Var
i,j:integer;
begin
with strGrid do
begin
for i:=fixedrows to rowcount-1 do
for j:=FixedCols to colcount-1 do
cells[j,i]:='';
end;
end;
procedure TForm1.RefreshInfo(var Msg: TMessage);
begin
RefreshStrGrid;
end;procedure TForm1.RefreshStrGrid;
var
sSql : String;
begin
try
Screen.Cursor := crSqlWait;
setstrGrid(StrGrid);
StrGrid.RowCount := 2;
sSql :=' Select stockname,location,locationflg,note from Dic_location where 1=1';
with ADOQuery1 do
begin
Close;
SQL.Text := sSql;
Open;
end;
SetGridInfo(StrGrid,ADOQuery1);
if StrGrid.RowCount>2 then StrGrid.RowCount := StrGrid.RowCount - 1;
finally
Screen.Cursor := crDefault;
end;
end;procedure TForm1.SetGridInfo(StrGrid:TStringGrid;sOraQuery:TADOQuery);
var
i,j:integer;
begin
StrGrid.RowCount:=2;
i:=1;
while not sOraQuery.eof do
begin
StrGrid.Cells[0,i]:=inttostr(i);
for j:=1 to sOraQuery.FieldCount do
begin
StrGrid.Cells[j,i]:=sOraQuery.Fields[j-1].AsString;
end;
i:=i+1;
StrGrid.RowCount:=StrGrid.RowCount+1;
sOraQuery.Next;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RefreshStrGrid;
end;
//这部分代码在Form2中调用,我这里没用第二个
procedure TForm1.Button2Click(Sender: TObject);
begin
SendMessage(Form1.handle,WM_LOCATION,0,0);
end;end.
应该用对像方式来实现
FORM1里有STRINGGIRD和BUTTON,STRINGGRID从ADOQUERY读取数据(SELECT * FROM XXX,点BUTTON时跳出FORM2,假设里面有2个EDIT,现在想要的是:把EDIT的值插入SQL,并刷新STRINGGIRD;插入可以实现,就是怎么刷新?
2.数据已经存在数据库里了,你在form2中再调用一次form1中stringgrid从ADOQUERY读取数据的那部分代码(把这部分做成一个过程,假如这部分过程为 getinfo;你在form2中调用时就写:form1.getinfo.这样就可以了
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids;type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if not assigned(form2) then
form2 := Tform2.Create(Application);
form2.ShowModal;
form2.Free;
form2 := Nil;
end;end.
//第二个单元
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm2 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementationuses Unit1;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
begin
//存入数据库
with adoquery1 do
begin
close;
sql.text := ' insert into t1 values('''+trim(edit1.text)+''','''+trim(edit2.text)+'''');
ExecSql;
end;
form1.StringGrid1.Cells[1,form1.StringGrid1.RowCount-1] := trim(edit1.Text);
form1.StringGrid1.Cells[2,form1.StringGrid1.RowCount-1] := trim(edit2.Text);
form1.StringGrid1.RowCount := form1.StringGrid1.RowCount+1;
end;end.