|
|
Data Grid Update Script
{This script uses Delphi to create a Grid showing multiple records into which you can update selected fields.
The practical example below is a grid that displays Item Sell and Buy Prices and allows you, in a single screen, to update selected Prices}
var
frmMain: TForm;
pnlTop: TPanel;
pnlBottom: TPanel;
btnClose: TButton;
btnOK: TButton;
lblField1: TLabel;
lblField2: TLabel;
lblField3: TLabel;
lblField4: TLabel;
edtField1: TEdit;
edtField2: TEdit;
dateField1: TDateTimePicker;
ComboField1: TComboBox;
TempQuery: TpFIBDataSet;
TempTrans: TpFIBTransaction;
dbgData: TDBGrid;
TempDS: TDatasource;
ItemDisplay,ItemPriceUpdate: String;
procedure CloseClick;
begin
TempQuery.edit;
TempQuery.post;
frmMain.close;
end;
begin
frmMain := TForm.Create(nil);
try
begin
frmMain.width := 760;
frmMain.height := 600;
frmMain.caption := 'Ostendo - Update Item Prices Grid (Immediate Update)';
frmMain.position := poScreenCenter;
frmMain.borderstyle := bsSizeable;
frmMain.name := 'MainForm';
TempTrans := TpFIBTransaction.Create(frmMain);
TempQuery := TpFIBDataSet.Create(frmMain);
TempTrans.DefaultDatabase := OstendoDB;
TempQuery.Database := OstendoDB;
TempQuery.Transaction := TempTrans;
TempQuery.Autocommit := true;
ItemDisplay := 'select ItemCode as "Item Code",' +
'itemdescription as "Description",' +
'itemunit as "Unit",' +
'lastcost as "Last Cost",' +
'standardcost as "Std Cost",' +
'averagecost as "Avg Cost",' +
'stdbuyprice as "Buy Price",' +
'stdsellprice as "Sell Price",' +
'sysuniqueid from Itemmaster order by 1';
ItemPriceUpdate := 'update Itemmaster set stdbuyprice = :"Buy Price",' +
'stdsellprice = :"Sell Price"' +
' where sysuniqueid = :old_sysuniqueid';
TempQuery.SelectSQL.add(ItemDisplay);
TempQuery.UpdateSQL.add(ItemPriceUpdate);
TempQuery.open;
TFloatField(TempQuery.FN('Last Cost')).currency := true;
TFloatField(TempQuery.FN('Avg Cost')).currency := true;
TFloatField(TempQuery.FN('Std Cost')).currency := true;
TFloatField(TempQuery.FN('Buy Price')).currency := true;
TFloatField(TempQuery.FN('Sell Price')).currency := true;
TempDS := TDatasource.create(frmMain);
TempDS.Dataset := TempQuery;
{Top Panel}
pnlTop := TPanel.Create(frmMain);
pnlTop.Parent := frmMain;
pnlTop.Align := alclient;
pnlTop.BevelOuter := bvLowered;
{Create the Data aware grid and set the column options}
dbgData := TDBGrid.create(frmMain);
dbgData.parent := pnlTop;
dbgData.align := alClient;
dbgData.datasource := TempDS;
dbgData.columns[0].width := 140;
dbgData.columns[0].readonly := true;
dbgData.columns[1].width := 200;
dbgData.columns[1].readonly := true;
dbgData.columns[2].width := 40;
dbgData.columns[2].readonly := true;
dbgData.columns[3].readonly := true;
dbgData.columns[4].readonly := true;
dbgData.columns[5].readonly := true;
dbgData.columns[8].visible := false; {Hide the Sysuniqueid field}
{Create the Bottom Panel with Buttons}
{Bottom Panel}
pnlBottom := TPanel.Create(frmMain);
pnlBottom.Parent := frmMain;
pnlBottom.Height := 50;
pnlBottom.width := 700;
pnlBottom.Align := albottom;
pnlBottom.BevelOuter := bvNone;
{Close Button}
btnClose := TButton.Create(pnlBottom);
btnClose.Parent := pnlBottom;
btnClose.left := 600;
btnClose.Top := 15;
btnClose.Width := 90;
btnClose.Height := 20;
btnClose.Caption := '&Close';
btnClose.OnClick := 'CloseClick';
btnClose.Hint := 'Close Price Update Screen';
btnClose.ShowHint := True;
btnClose.taborder := 99;
btnClose.anchors := akright;
frmMain.showmodal;
end;
finally
frmMain.free;
end;
end.