[DEPLACE --> Delphi] stingGrid et dbgrid

zerargui Messages postés 65 Date d'inscription vendredi 24 octobre 2008 Statut Membre Dernière intervention 25 février 2019 - 7 nov. 2008 à 07:48
zerargui Messages postés 65 Date d'inscription vendredi 24 octobre 2008 Statut Membre Dernière intervention 25 février 2019 - 7 nov. 2008 à 07:54
j'ai pris un petit programme de Delphiprog en le modifiant par la lecture des indexes de bases .DB à la place des bases .dbf tous s'affiche normalement sauf lorsqu'on click sur les indexexs ou rien ne se passe au niveau du Tri. Help me please je un débutant . voici le code :

unit Unit2;

interface

uses
  Windows, Messages, Classes, SysUtils, Graphics, Controls, StdCtrls, Forms,
  Dialogs, DBCtrls, DB, DBGrids, Grids, DBTables;

type
  TForm2 = class(TForm)
    Table1CodeCli: TStringField;
    Table1NomCli: TStringField;
    Table1PrenomCli: TStringField;
    Table1AdresseCli: TStringField;
    Table1AgeCli: TStringField;
    Table1TelCli: TStringField;
    Table1MtCdes: TFloatField;
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    ListBox1: TListBox;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure FormDestroy(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);

  private
    { déclarations privées }
  public
    { déclarations publiques }
  end;

var
  Form2: TForm2;
  //Liste des index secondaires disponibles
  IndexList: TStrings;
  //Liste des index secondaires créés à la volée
  CreatedIndexList: TStrings;

implementation

{$R *.DFM}

procedure TForm2.FormCreate(Sender: TObject);
begin
  Table1.Open;
  //Récupérer la liste des index déclarés
  //avant le lancement du programme
  Table1.GetIndexNames(IndexList);
  //En faire une copie pour l'afficher et
  //permettre une sélection par l'utilisateur
  ListBox1.Items.Assign(IndexList);

end;

procedure TForm2.DBGrid1TitleClick(Column: TColumn);
begin
  //Vérifie que la colonne affiche des données de la
  //table et non un champ calculé
  if DBGrid1.ValidFieldIndex(Column.Index) then
  begin
    //Un index correspondant à la colonne cliquée existe-t-il ?
    if IndexList.IndexOf(Column.FieldName) <> -1 then
      //Si oui, l'activer
      with TTable(DbGrid1.DataSource.DataSet) do
        IndexFieldNames := Column.FieldName
    else
      //L'index n'existe pas :
      showmessage('l''indexe n''existe pas');
      with TTable(DbGrid1.DataSource.DataSet) do
      begin
        Close;
        Exclusive := True;
        //créer un index secondaire correspondant
        //à la colonne cliquée
        try
          AddIndex(Column.FieldName, Column.FieldName, []);
          //rendre actif l'index créé
          IndexFieldNames := Column.FieldName;
          Exclusive := False;
          Open;
          //Réactualiser la liste des index de la table
          GetIndexNames(IndexList);
          //et l'afficher
          ListBox1.Items.Assign(IndexList);
          CreatedIndexList.Assign(IndexList);
        except
          on EDatabaseError do
          begin
            ShowMessageFmt('Impossible de créer un index'#13'sur le champ %s.',
              [Column.FieldName]);
            IndexFieldNames := '';
            Exclusive := False;
            if not Active then
              Open;
          end;
        end;
      end;
  end;

end;

procedure TForm2.FormDestroy(Sender: TObject);
var
  i: integer;
begin
  with Table1 do
  begin
    Close;
    Exclusive := True;
    for i := 0 to CreatedIndexList.Count - 1 do
    try
      Table1.DeleteIndex(CreatedIndexList.Strings[i]);
    except
      //
    end;
    Exclusive := False;
    Close;
  end;
end;

procedure TForm2.ListBox1Click(Sender: TObject);
begin
  //S'il y a au moins un élément dans la liste
  with ListBox1 do
    if Items.Count > 0 then
      //Rendre actif l'index correspondant à
      //l'élément sélectionné
      Table1.IndexFieldNames := Items[ItemIndex];
end;

procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Column.FieldName = Table1.IndexFieldNames then
    //Colorer en bleu le titre cliqué
    Column.Title.Font.Color := clBlue
  else
    Column.Title.Font.Color := clTeal;

end;

initialization
  IndexList := TStringList.Create;
  CreatedIndexList := TStringList.Create;

finalization
  IndexList.Free;
  CreatedIndexList.Free;
end.

Cordialement Amar

1 réponse

zerargui Messages postés 65 Date d'inscription vendredi 24 octobre 2008 Statut Membre Dernière intervention 25 février 2019
7 nov. 2008 à 07:54
NB: aucune exception n'apparait.
0
Rejoignez-nous