unit UterritoireSELECT;

interface

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

type
  TFrmterritoireSELECT = class(TForm)
    Panel1: TPanel;
    GridClient: TDBGrid;
    Dsetatterritorial: TDataSource;
    BtnOk: TBitBtn;
    Panel5: TPanel;
    eCode: TEdit;
    Label1: TLabel;
    PopupMenuGrid: TPopupMenu;
    Ajouter1: TMenuItem;
    Modifier1: TMenuItem;
    Supprimer1: TMenuItem;
    procedure FiltrerAffichage();
    procedure eCodeChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnOkClick(Sender: TObject);
    procedure GridClientKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnSupprimerClick(Sender: TObject);
    procedure eCodeKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmterritoireSELECT: TFrmterritoireSELECT;

implementation

{$R *.dfm}
uses UDATAPRIX, umain;
procedure TFrmterritoireSELECT.FiltrerAffichage();
var asFiltre :String;
begin
asFiltre := '';
if(eCode.Text <> '') then
   asFiltre := 'upper(code) LIKE ' + QuotedStr('%'+uppercase(eCode.Text) +'%');

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

procedure TFrmterritoireSELECT.eCodeChange(Sender: TObject);
begin
FiltrerAffichage();
if(DATAPRIX.Etatterritorial.RecordCount = 0)then
  ActiveControl := eCode;
end;

procedure TFrmterritoireSELECT.FormClose(Sender: TObject;
  var Action: TCloseAction);
  begin
  frmmain.libelle:=DATAPRIX.Etatterritorialcode.value;
  frmmain.libelle1:=DATAPRIX.EtatterritorialPAYS.value;
Action := caFree;
end;

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

procedure TFrmterritoireSELECT.GridClientKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if(Key in [VK_RETURN]) then
   BtnOkClick(Sender);
if((Key = VK_UP) and (GridClient.DataSource.DataSet.RecNo = 1)) then
  eCode.SetFocus;
end;

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

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

procedure TFrmterritoireSELECT.FormShow(Sender: TObject);
begin
DATAPRIX.Etatterritorial.Close;
DATAPRIX.Etatterritorial.sql.clear;
DATAPRIX.Etatterritorial.sql.Add('select * from ETATTERRITORIAL order by code');
DATAPRIX.Etatterritorial.OPEN;
end;

end.
