IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

StringGrid sort


Sujet :

C++Builder

  1. #1
    Membre habitué
    Inscrit en
    Juin 2002
    Messages
    198
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 198
    Points : 139
    Points
    139
    Par défaut [Resolu]StringGrid sort
    Bonjour,
    Est-il possible (simplement) de faire un sort() sur une colonne d'un StringGrid et ensuite de déplacer les lignes selon le résultat?
    Merci

  2. #2
    Membre habitué
    Avatar de Alacazam
    Profil pro
    Inscrit en
    Septembre 2002
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 88
    Points : 167
    Points
    167
    Par défaut
    Salut,
    J'ai cherché en vain de quoi trier selon une colonne (cf Excel)

    Le mieux est donc de repasser par un ListBox (pour éviter de faire toi-même le tri) qui admet la propriété Sorted.

    Tu remplis ton ListBox avec des chaines contenant chaque ligne, ta colonne en question en premier ...
    ... et après tu reconstruit ta grille !
    N.B. Mets le ListBox en Visible = false;

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    ListBox1->Items->Clear();
    ListBox1->Sorted = false;
    for (int i = 0 ; i < StringGrid1->RowCount ; i++) {
      ListBox1->Items->Add(StringGrid1->Cells[2][i]
          +"#"+StringGrid1->Cells[0][i]+"#"
          +StringGrid1->Cells[1][i]);
    	}
    ListBox1->Sorted = true;
    int a = 0;
    AnsiString s, t;
    for (int i = 0 ; i < ListBox1->Items->Count ; i++) {
    	s = ListBox1->Items->Strings[i]+"#";
        for (int j = 0 ; j < 3 ; j++) {
            a = s.Pos("#");
            t = s.SubString(1, a-1);
            s = s.SubString(a+1, s.Length());
    		if (j == 0)
            	 StringGrid1->Cells[2][i] = t;
    		if (j == 1)
            	 StringGrid1->Cells[0][i] = t;
    		if (j == 2)
            	 StringGrid1->Cells[1][i] = t;
    		}
    	}
    Evidemment, tu peux mettre autre chose que "#" comme séparateur si ce caractère te sert ...
    J'espère que ça ira ...

    Mathieu
    Développeur confirmé C++, Java, Python, JS.

  3. #3
    Membre habitué
    Inscrit en
    Juin 2002
    Messages
    198
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 198
    Points : 139
    Points
    139
    Par défaut
    Merci beaucoup !!

  4. #4
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Fonction pour Trier un StringGrid
    Salut à tous,
    Voici un petit bout de code permettant de Trier un StringGrid sans passer par un listbox.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    void __fastcall TForm1::Trier(void)
    {
     int idxd;
     int idxa;
     
     char NoSwap = false;
     
     AnsiString SWAP_AR_REF;
     AnsiString SWAP_DL_DESIGN;
     AnsiString SWAP_DATE;
     AnsiString SWAP_QTE;
     AnsiString SWAP_CLIENT;
     AnsiString SWAP_VALORISE;
     AnsiString SWAP_PRIXUNIT;
     AnsiString SWAP_PRIXTTC;
     AnsiString SWAP_NUM;
     AnsiString SWAP_DEPOT;
     AnsiString SWAP_ORDRE;
     AnsiString SWAP_OK;
     
     for (idxd=1;idxd<StringGrid2->RowCount-1;idxd++)
      {
       NoSwap = 1;
     
       for (idxa=StringGrid2->RowCount-1;idxa>1;idxa--)
        {
         if (StringGrid2->Cells[1][idxa-1] > StringGrid2->Cells[1][idxa])
          {
           // sauve les donnée de l'index inferieur...
           SWAP_AR_REF            = StringGrid2->Cells[1][idxa-1];
           SWAP_DL_DESIGN         = StringGrid2->Cells[2][idxa-1];
           SWAP_DATE              = StringGrid2->Cells[3][idxa-1];
           SWAP_QTE               = StringGrid2->Cells[4][idxa-1];
           SWAP_CLIENT            = StringGrid2->Cells[5][idxa-1];
           SWAP_VALORISE          = StringGrid2->Cells[6][idxa-1];
           SWAP_PRIXUNIT          = StringGrid2->Cells[7][idxa-1];
           SWAP_PRIXTTC           = StringGrid2->Cells[8][idxa-1];
           SWAP_NUM               = StringGrid2->Cells[9][idxa-1];
           SWAP_DEPOT             = StringGrid2->Cells[10][idxa-1];
           SWAP_ORDRE             = StringGrid2->Cells[11][idxa-1];
           SWAP_OK                = StringGrid2->Cells[12][idxa-1];
           // transfere les données de l'index supperieur->inferieur
           StringGrid2->Cells[1][idxa-1]           = StringGrid2->Cells[1][idxa];
           StringGrid2->Cells[2][idxa-1]           = StringGrid2->Cells[2][idxa];
           StringGrid2->Cells[3][idxa-1]           = StringGrid2->Cells[3][idxa];
           StringGrid2->Cells[4][idxa-1]           = StringGrid2->Cells[4][idxa];
           StringGrid2->Cells[5][idxa-1]           = StringGrid2->Cells[5][idxa];
           StringGrid2->Cells[6][idxa-1]           = StringGrid2->Cells[6][idxa];
           StringGrid2->Cells[7][idxa-1]           = StringGrid2->Cells[7][idxa];
           StringGrid2->Cells[8][idxa-1]           = StringGrid2->Cells[8][idxa];
           StringGrid2->Cells[9][idxa-1]           = StringGrid2->Cells[9][idxa];
           StringGrid2->Cells[10][idxa-1]           = StringGrid2->Cells[10][idxa];
           StringGrid2->Cells[11][idxa-1]           = StringGrid2->Cells[11][idxa];
           StringGrid2->Cells[12][idxa-1]           = StringGrid2->Cells[12][idxa];
           // restore les donnee sauvée dans l'index supperieur
     
            StringGrid2->Cells[1][idxa] =  SWAP_AR_REF;
            StringGrid2->Cells[2][idxa] =  SWAP_DL_DESIGN;
            StringGrid2->Cells[3][idxa] =  SWAP_DATE;
            StringGrid2->Cells[4][idxa] =  SWAP_QTE;
            StringGrid2->Cells[5][idxa] =  SWAP_CLIENT;
            StringGrid2->Cells[6][idxa] =  SWAP_VALORISE;
            StringGrid2->Cells[7][idxa] =  SWAP_PRIXUNIT;
            StringGrid2->Cells[8][idxa] =  SWAP_PRIXTTC;
            StringGrid2->Cells[9][idxa] =  SWAP_NUM;
            StringGrid2->Cells[10][idxa] = SWAP_DEPOT;
            StringGrid2->Cells[11][idxa] = SWAP_ORDRE;
            StringGrid2->Cells[12][idxa] = SWAP_OK;
     
           NoSwap = 0;
          }
        }
        if (NoSwap == 1)
         {
          break;
         }
      }
    }
    Ce code vous permet de trier un StringGrid, mettez vos propres valeur StringGrid et rajouter des colonnes si vous en avez besoins.

    Bon Code à tous !
    Knar

  5. #5
    Membre habitué
    Inscrit en
    Juin 2002
    Messages
    198
    Détails du profil
    Informations forums :
    Inscription : Juin 2002
    Messages : 198
    Points : 139
    Points
    139
    Par défaut
    Bonsoir,
    Dans mon programme j'ai utilisé un Tlist et ,ca marche très bien.
    Ça fait déjà qque temps que mon programme tourne et j'avoue avoir un peu oublié cette demande d'aide.
    Je te remercie de ta réponse.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. StringGrid, multiligne et word wrap
    Par Dajon dans le forum C++Builder
    Réponses: 7
    Dernier message: 17/09/2003, 17h15
  2. Déplacer la sélection d'une ligne dans un stringgrid
    Par jer64 dans le forum Composants VCL
    Réponses: 5
    Dernier message: 14/03/2003, 00h57
  3. JBuilder 7 personnal sort à chaque save
    Par Hannouz dans le forum JBuilder
    Réponses: 4
    Dernier message: 17/12/2002, 22h53
  4. Multi lignes dans un StringGrids ?
    Par Xavier dans le forum C++Builder
    Réponses: 3
    Dernier message: 27/11/2002, 23h15
  5. StringGrid et colonnes
    Par Delph dans le forum Composants VCL
    Réponses: 2
    Dernier message: 02/08/2002, 11h35

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo