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# Discussion :

remplir un datagridview avec une collection


Sujet :

C#

  1. #1
    Membre à l'essai
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Mars 2015
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet Informatique

    Informations forums :
    Inscription : Mars 2015
    Messages : 13
    Points : 10
    Points
    10
    Par défaut remplir un datagridview avec une collection
    Bonjour,

    Je souhaite remplir un datagridview avec une collection en passant par une datasource.
    mon problème c'est qu'il n'y a rien qui s'affiche.
    Pourtant en debugant, je constate que la datasource est remplie.

    Nom : 2015-06-02_081233.png
Affichages : 519
Taille : 8,3 Ko

    voici mon code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
                dataGridView_command.AutoGenerateColumns = true;
     
                commandBindingSource = new BindingSource();
     
                List<Command> cmds = new List<Command>();
     
                foreach (Command c in FormMain.profile.commandList)
                {
                    cmds.Add(c);
                }
     
                commandBindingSource.DataSource = cmds;
                dataGridView_command.DataSource = commandBindingSource;
    je ne comprend pas pourquoi rien ne s'affiche.
    ma collection command ressemble à ça :

    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
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    namespace Hydra {
        [Serializable]
        public class Command {
     
            public string commandName;
            public string commandString;
            public System.Windows.Forms.Keys commandKey;
            public System.Windows.Forms.Keys commandKeyModifier;
            public bool commandKeyInhibition;
            public bool commandKeyPush;
            public bool commandKeyRelease;
            public string commandKeySeries;
            public System.Windows.Forms.MouseButtons commandMouse;
            public string commandJoystick;
            public string commandJoystickSeries;
            public string commandGamepad;
            public string commandGamepadSeries;
            public string commandDescription;
            public string commandCategorie;
            public string commandType;
            public string commandGroup;
            public string commandSubType;
            public int commandRepetitions;
            public bool commandAutorisationOther;
            public bool commandAutorisationForMe;
            public List<Actions> actionList;
     
     
     
            public Command() {
            }
     
            public Command(string commandName, 
                           string commandString, 
                           System.Windows.Forms.Keys commandKey, 
                           System.Windows.Forms.Keys commandKeyModifier, 
                           bool commandKeyInhibition, 
                           bool commandKeyPush, 
                           bool commandKeyRelease, 
                           string commandKeySeries,
                           System.Windows.Forms.MouseButtons commandMouse, 
                           string commandJoystick,
                           string commandJoystickSeries, 
                           string commandGamepad,
                           string commandGamepadSeries, 
                           string commandDescription, 
                           string commandCategorie, 
                           string commandType,
                           string commandGroup,
                           string commandSubType,
                           int commandRepetitions,
                           bool commandAutorisationOther,
                           bool commandAutorisationForMe,
                           List<Actions> actionList)
            {
                this.commandName = commandName;
                this.commandString = commandString;
                this.commandKey = commandKey;
                this.commandKeyModifier = commandKeyModifier;
                this.commandKeyInhibition = commandKeyInhibition;
                this.commandKeyPush = commandKeyPush;
                this.commandKeyRelease = commandKeyRelease;
                this.commandKeySeries = commandKeySeries;
                this.commandMouse = commandMouse;
                this.commandJoystick = commandJoystick;
                this.commandJoystickSeries = commandJoystickSeries;
                this.commandGamepad = commandGamepad;
                this.commandGamepadSeries = commandGamepadSeries;
                this.commandDescription = commandDescription;
                this.commandCategorie = commandCategorie;
                this.commandType = commandType;
                this.commandGroup = commandGroup;
                this.commandSubType = commandSubType;
                this.commandRepetitions = commandRepetitions;
                this.commandAutorisationOther = commandAutorisationOther;
                this.commandAutorisationForMe = commandAutorisationForMe;
                this.actionList = actionList;
            }
     
            public override string ToString() {
                string returnString = commandName + " : " + actionList.Count.ToString();
                if (actionList.Count > 1) {
                    returnString += " actions";
                }
                else {
                    returnString += " action";
                }
     
                return returnString;
            }

    merci d'avance pour votre aide

  2. #2
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Essaye de remplacer
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    List<Command> cmds = new List<Command>();
    par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    BindingList<Command> cmds = new BindingList<Command>();

  3. #3
    Membre à l'essai
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Mars 2015
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet Informatique

    Informations forums :
    Inscription : Mars 2015
    Messages : 13
    Points : 10
    Points
    10
    Par défaut
    toujours pas ;-(

  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Il me semble que les columns bindées sont seulement les properties (pas les variables) , donc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    public string CommandName { get { return commandName ; } }
    public string CommandString { get { return commandString ; } }
    ...

  5. #5
    Membre à l'essai
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Mars 2015
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet Informatique

    Informations forums :
    Inscription : Mars 2015
    Messages : 13
    Points : 10
    Points
    10
    Par défaut
    je crois que j'ai trouvé la solution ici :

    http://tech.pro/tutorial/776/csharp-...o-a-collection

    c'est très proche de ta solution je pense. J'ai remarqué la majuscule qui change tout dans ton exemple ;-)

    je test tout ça ce soir

  6. #6
    Membre à l'essai
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Mars 2015
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet Informatique

    Informations forums :
    Inscription : Mars 2015
    Messages : 13
    Points : 10
    Points
    10
    Par défaut
    Coooooool ;-)

    par contre honnêtement, je n'ai pas compris ce que j'ai fait ça sert à quoi ça ? :

    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
     
     
            public string commandName
            {
                get { return _commandName; }
                set { _commandName = value; }
            }
     
            public string commandString
            {
                get { return _commandString; }
                set { _commandString = value; }
            }
     
    ...

  7. #7
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Dans le cas présent, ça ne sert à rien à part le fait que seules les properties sont "bindables".

    De façon plus générale, une property permet :

    en lecture
    de déduire une valeur à partir des autres variables de la classe.
    Par exemple, à partir d'es 2 variable "Nom" et "Prenom" de la classe, on peut définir une propriété NomComplet.

    en écriture
    On peut affecter à une ou plusieurs variables de la base des valeurs déduites de la valeur d'entréée.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    public string NomComplet 
    { 
      get { return Prenom+" "+Nom ; } 
      set { string[] Words=value.Split(' ') ; Prenom=Words[0] ; Nom=Words.Length==0 ? "":Words[1] ; }
    }
    Pour une property, on peut définir le set et le get, ou alors seulement le get ou le set.
    Une property public permet de pouvoir lire certaines variables private, sans pouvoir les modifier si on ne définit pas le "set".

  8. #8
    Membre à l'essai
    Homme Profil pro
    Chef de projet Informatique
    Inscrit en
    Mars 2015
    Messages
    13
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet Informatique

    Informations forums :
    Inscription : Mars 2015
    Messages : 13
    Points : 10
    Points
    10
    Par défaut
    merci à toi. je comprend mieux pourquoi ça fonctionnait partout dans mon code sauf pour cette partie.

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

Discussions similaires

  1. Remplir un DataGridView avec une classe
    Par wissem.ba dans le forum Débuter
    Réponses: 2
    Dernier message: 12/05/2014, 15h43
  2. Remplir un DataGridView avec une classe
    Par wissem.ba dans le forum C#
    Réponses: 1
    Dernier message: 07/05/2014, 18h15
  3. Remplir un DataTable avec une collection (Dictionnaire)
    Par dragondumond dans le forum VB.NET
    Réponses: 7
    Dernier message: 04/06/2013, 13h18
  4. Remplir un Datagridview avec une requête Postgres
    Par robertisaline dans le forum VB.NET
    Réponses: 1
    Dernier message: 02/02/2012, 08h24
  5. Remplir un combobox avec une collection Dictionnary
    Par kodo dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 19/06/2010, 19h21

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