unit uetatterritoire;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, Grids, DBGrids, ExtCtrls, Menus, Mask,
  ToolEdit, dxCore, dxButton;

type
  TFrmetatterritoire = class(TForm)
    Panel1: TPanel;
    Dsirepertoire: TDataSource;
    BtnOk: TBitBtn;
    PopupMenuGrid: TPopupMenu;
    Ajouter1: TMenuItem;
    Modifier1: TMenuItem;
    Supprimer1: TMenuItem;
    Panel7: TPanel;
    LabChpRech: TLabel;
    eValChampRech: TEdit;
    DBGrid1: TDBGrid;
    dxButton1: TdxButton;
    dxButton4: TdxButton;
    dxButton5: TdxButton;
    dxButton2: TdxButton;
    procedure FiltrerAffichage();
    procedure eCodeChange(Sender: TObject);
     
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnOkClick(Sender: TObject);

    procedure BtnSupprimerClick(Sender: TObject);
    procedure eCodeKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Ajouter1Click(Sender: TObject);
    procedure Modifier1Click(Sender: TObject);
    procedure eValChampRechChange(Sender: TObject);
    
    procedure DBGrid1DblClick(Sender: TObject);
    procedure DBGrid1Enter(Sender: TObject);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormCreate(Sender: TObject);
    procedure DBGridNewWindowProc(var Msg: TMessage);
    procedure dxButton2Click(Sender: TObject);
  private
   OldWindowProc: TWndMethod;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Frmetatterritoire: TFrmetatterritoire;
  aNomChampRech :String;
implementation

{$R *.dfm}
uses udataprix;

procedure TFrmetatterritoire.FiltrerAffichage();
var asFiltre :String;
begin
if LabChpRech.Caption= 'Code' then
 begin
asFiltre := '';
if(eValChampRech.Text <> '') then
   asFiltre := 'upper(code) LIKE ' + QuotedStr('%'+uppercase(eValChampRech.Text) +'%');

dataprix.Etatterritorial.Close;
dataprix.Etatterritorial.SQL.Clear;
if asfiltre <>'' then
dataprix.Etatterritorial.SQL.Add('select * from Etatterritorial where '+asFiltre
                                  + ' order by code')
else
dataprix.Etatterritorial.SQL.Add('select * from Etatterritorial '
                               + ' order by code');
dataprix.Etatterritorial.open;
end;

if LabChpRech.Caption= 'Pays' then
 begin
asFiltre := '';
if(eValChampRech.Text <> '') then
   asFiltre := 'upper(Pays) LIKE ' + QuotedStr('%'+uppercase(eValChampRech.Text) +'%');

dataprix.Etatterritorial.Close;
dataprix.Etatterritorial.SQL.Clear;
if asfiltre <>'' then
dataprix.Etatterritorial.SQL.Add('select * from Etatterritorial where '+asFiltre
                                  + ' order by pays')
else
dataprix.Etatterritorial.SQL.Add('select * from Etatterritorial '
                               + ' order by pays');
dataprix.Etatterritorial.open;
end;

end;

procedure TFrmetatterritoire.eCodeChange(Sender: TObject);
begin
FiltrerAffichage();
if(dataprix.Etatterritorial.RecordCount = 0)then
  ActiveControl := eValChampRech;
end;


procedure TFrmetatterritoire.FormClose(Sender: TObject;
  var Action: TCloseAction);
  begin
Action := caFree;
end;

procedure TFrmetatterritoire.BtnOkClick(Sender: TObject);
begin
ModalResult := mrOk;
end;

procedure TFrmetatterritoire.BtnSupprimerClick(Sender: TObject);
begin
if(Application.MessageBox('Voulez-vous supprimer la ligne en cours ?', 'Confirmation',MB_YESNO)= IDYES) then
   DsIrepertoire.DataSet.Delete;
end;

procedure TFrmetatterritoire.eCodeKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(Key = VK_DOWN) then
    DBGrid1.SetFocus;
end;

procedure TFrmetatterritoire.FormShow(Sender: TObject);
begin
dataprix.Etatterritorial.Close;
dataprix.Etatterritorial.OPEN;
 
end;

procedure TFrmetatterritoire.Ajouter1Click(Sender: TObject);
begin
 dataprix.Etatterritorial.Append;
 DBGrid1.ReadOnly:=False;
 DBGrid1.SetFocus;
end;

procedure TFrmetatterritoire.Modifier1Click(Sender: TObject);
begin
 dataprix.Etatterritorial.Edit;
 DBGrid1.ReadOnly:=False;
end;

procedure TFrmetatterritoire.eValChampRechChange(Sender: TObject);
begin
FiltrerAffichage();
ActiveControl := eValChampRech;
end;

procedure TFrmetatterritoire.DBGrid1DblClick(Sender: TObject);
begin
// dataprix.Etatterritorial.Edit;
end;

procedure TFrmetatterritoire.DBGrid1Enter(Sender: TObject);
begin

if  dataprix.Etatterritorial.State in [dsedit,dsinsert] then
    begin
    dataprix.Etatterritorial.post;
    dataprix.Etatterritorial.Refresh;
    end;
dataprix.Etatterritorial.Edit;
DBGrid1.SetFocus;
end;

procedure TFrmetatterritoire.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if(Key in [VK_F10	]) then
    ModalResult := mrOk;  //F10
if (key in [vk_up]) and (dataprix.Etatterritorialcode.Value='') then
dataprix.Etatterritorial.Cancel;

if(Key in [VK_RETURN]) then
case DBGrid1.SelectedIndex of
     0:DBGrid1.SelectedIndex:=DBGrid1.SelectedIndex+1;

     1:begin

             if dataprix.Etatterritorial.State in [dsbrowse] then
                dataprix.Etatterritorial.Edit;
             if dataprix.Etatterritorial.State in [dsinsert,dsedit] then
                begin
                 dataprix.Etatterritorial.Post;
                 DBGrid1.ReadOnly:=True;

                end;
              DBGrid1.SelectedIndex:=0;
            end;

end;//end case
end;

procedure TFrmetatterritoire.DBGrid1TitleClick(Column: TColumn);
begin
DBGrid1.Columns[0].Color := $00FFFFF4 ;
DBGrid1.Columns[1].Color := $00FFFFF4 ;

   dataprix.Etatterritorial.Close;
   dataprix.Etatterritorial.SQL.Clear;
   dataprix.Etatterritorial.SQL.Add('select * from Etatterritorial'
                                     + ' Order by ' +  Column.FieldName);
   aNomChampRech := Column.FieldName;
   LabChpRech.Caption := Column.Title.Caption;
   dataprix.Etatterritorial.Open;
   Column.Color :=  $00B3D9FF;
end;

procedure TFrmetatterritoire.DBGrid1CellClick(Column: TColumn);
begin
if dataprix.Etatterritorial.State in [dsbrowse] then
   dataprix.Etatterritorial.Edit;
end;

procedure TFrmetatterritoire.FormCreate(Sender: TObject);
begin
 OldWindowProc := Dbgrid1.WindowProc;
 Dbgrid1.WindowProc := DBGridNewWindowProc;
end;

procedure TFrmetatterritoire.DBGridNewWindowProc(var Msg: TMessage);
begin
  //.Interception de l'évènement WM_MOUSEWHEEL. 
  if Msg.Msg = WM_MOUSEWHEEL then 
  begin 
    if (Dbgrid1.DataSource.DataSet.Active) then
    begin 
      if SmallInt(Msg.WParamHi) < 0 then
        Dbgrid1.DataSource.DataSet.Next
      else
        Dbgrid1.DataSource.DataSet.Prior;
    end; 
    Exit; 
  end; 
  
  //.Traitement normal des autres message. 
  OldWindowProc(Msg); 
end;

procedure TFrmetatterritoire.dxButton2Click(Sender: TObject);
begin
  close;
end;

end.
