1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| unit DBGridKeyInc_ADO_MainForm;
interface
uses
(*Winapi.*)Windows, (*Winapi.*)Messages, (*System.*)SysUtils, (*System.*)Variants, (*System.*)Classes, (*Vcl.*)Graphics,
(*Vcl.*)Controls, (*Vcl.*)Forms, (*Vcl.*)Dialogs, (*Data.*)DB, (*Vcl.*)ExtCtrls, (*Vcl.*)DBCtrls,
(*Vcl.*)Grids, (*Vcl.*)DBGrids, (*Datasnap.*)DBClient, (*Vcl.*)StdCtrls,
Vcl.Buttons, (*Data.Win.*)ADODB;
type
TDBGridKeyIncADOMainForm = class(TForm)
DBGrid: TDBGrid;
pnlMisc: TPanel;
edTotal: TLabeledEdit;
DBNavigator: TDBNavigator;
DataSource: TDataSource;
ADOQuery: TADOQuery;
pnlTop: TPanel;
edUser: TEdit;
edPassword: TEdit;
edServerName: TEdit;
edDataBaseName: TEdit;
procedure FormCreate(Sender: TObject);
procedure ADOQueryCalcFields(DataSet: TDataSet);
procedure DBGridKeyPress(Sender: TObject; var Key: Char);
procedure ADOQueryAfterPost(DataSet: TDataSet);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;
implementation
{$R *.dfm}
procedure TDBGridKeyIncADOMainForm.FormCreate(Sender: TObject);
begin
with TIntegerField.Create(ADOQuery)do
begin
FieldName := 'Code';
FieldKind := fkData;
DataSet := ADOQuery;
end;
with TStringField.Create(ADOQuery)do
begin
FieldName := 'Reference';
FieldKind := fkData;
Size := 10;
DataSet := ADOQuery;
end;
with TStringField.Create(ADOQuery)do
begin
FieldName := 'Produit';
FieldKind := fkData;
Size := 50;
DataSet := ADOQuery;
end;
with TIntegerField.Create(ADOQuery)do
begin
FieldName := 'Qte';
FieldKind := fkData;
DataSet := ADOQuery;
end;
with TFloatField.Create(ADOQuery)do
begin
FieldName := 'PU';
FieldKind := fkData;
DataSet := ADOQuery;
end;
with TFloatField.Create(ADOQuery) do
begin
FieldName := 'Total';
FieldKind := fkCalculated;
DataSet := ADOQuery;
ReadOnly := True;
end;
ADOQuery.ConnectionString := Format('Provider=%s;Persist Security Info=False;Data Source=%s;Initial Catalog=%s;User ID=%s;Password=%s', ['SQLNCLI11.1', edServerName.Text, edDataBaseName.Text, edUser.Text, edPassword.Text]);
ADOQuery.SQL.Text := 'SELECT Code ,Reference ,Produit ,Qte ,PU FROM AAA_SLT_07 WHERE CMD = 0';
ADOQuery.Open();
end;
procedure TDBGridKeyIncADOMainForm.ADOQueryCalcFields(DataSet: TDataSet);
var
Total: TField;
begin
Total := DataSet.FieldByName('Total');
Total.ReadOnly := False;
try
if not DataSet.FieldByName('PU').IsNull and not DataSet.FieldByName('Qte').IsNull then
Total.AsFloat := DataSet.FieldByName('PU').AsFloat * DataSet.FieldByName('Qte').AsInteger
else
Total.Clear();
finally
Total.ReadOnly := True;
end;
end;
procedure TDBGridKeyIncADOMainForm.DBGridKeyPress(Sender: TObject; var Key: Char);
var
DataSet: TDataSet;
Qte: TField;
begin
if (Key = '+') or (Key = '-') then
begin
// A voir si le caractère + iou - peuvent être dans une référence, donc prévoir de limiter selon la colonne active
//if DBGrid.SelectedField.FieldName = 'Qte' then
begin
DataSet := DBGrid.DataSource.DataSet;
Qte := DataSet.FieldByName('Qte');
// Dans le cas de cette modification sans avoir écrit le moindre caractère sur la ligne, le AutoEdit du TDataSource ne fait pas effet
if not (DataSet.State in [dsEdit, dsInsert]) then
DataSet.Edit();
Qte.AsInteger := Qte.AsInteger + 44 - Ord(Key);
ADOQueryCalcFields(DBGrid.DataSource.DataSet);
Key := #0;
end;
end;
end;
procedure TDBGridKeyIncADOMainForm.ADOQueryAfterPost(DataSet: TDataSet);
var
mark: TBookmark;
Total: Double;
PU, Qte: TField;
begin
edTotal.Text := '';
with DataSet do
begin
PU := FieldByName('PU');
Qte := FieldByName('Qte');
DisableControls();
try
mark := GetBookmark();
try
Total := 0;
First();
while not Eof do
begin
Total := Total + PU.AsFloat * Qte.AsInteger;
Next();
end;
edTotal.Text := FloatToStr(Total);
finally
GotoBookmark(mark);
end;
finally
EnableControls();
end;
end;
end;
end. |