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 :

Besoin d'aide pour mon code C# (DataGrid vide) [Débutant]


Sujet :

C#

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2014
    Messages : 3
    Points : 4
    Points
    4
    Par défaut Besoin d'aide pour mon code C# (DataGrid vide)
    Bonjour,

    Débutant dans la programmation, je souhaite afficher mes données dans un DataGrid suivant le Modèle MVC. Seulement mes données n'apparaissent pas dans le DataGrid. Ne comprenant pas tous à fait le passage de Données avec le Binding, je pense que le problème vient de là car tous mon code semble "correcte" pour moi, je n'ai pas d'erreurs et j'ai regarder un peu partout sur le web pour comprendre comment lié mon DataGrid à ma source de donnée mais impossible :s.

    Merci d'avance

    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
     
    private void Button_Click(object sender, RoutedEventArgs e)
           {
     
     
               test = new Collection<Habilitation>();
     
               HabilitationDao listeHabilitationCuid = new HabilitationDao();
               test = listeHabilitationCuid.rechercheHabilitation(cuid);
     
               this.DataContext = test;
     
           }//fin méthode
    XAML
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <DataGrid ItemsSource="{Binding ElementName=test}" Margin="40" FontSize="15" Grid.Column="2" Grid.RowSpan="2" AlternatingRowBackground="LightBlue">
                       <DataGrid.Columns>
                           <DataGridTextColumn Binding="{Binding code}" x:Name="dataGrid_codehabilitation" Header="Code Habilitation" />
                           <DataGridTextColumn Binding="{Binding nom}" x:Name="dataGrid_nom" Header="Nom"  />
                           <DataGridTextColumn Binding="{Binding prenom}" x:Name="dataGrid_prenom" Header="Prénom"  />
                           <DataGridTextColumn Binding="{Binding applicationLibelle}" x:Name="dataGrid_application" Header="Application" />
                           <DataGridTextColumn Binding="{Binding perimetreLibelle}" x:Name="dataGrid_perimetre" Header="Périmètre" />
                           <DataGridTextColumn Binding="{Binding dateHabilitation}" x:Name="dataGrid_dateHabilitation" Header="Date d'Habilitation" />
                           <DataGridTextColumn Binding="{Binding habiliteur}" x:Name="dataGrid_habiliteur" Header="Habiliteur" />
                       </DataGrid.Columns>
                   </DataGrid>
    Class HabilitationDao

    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
     
    //Liste des habilitations pour un CUID
           public Collection<Habilitation> rechercheHabilitation(string cuid)
           {
               Collection<Habilitation> listHabilitation = new Collection<Habilitation>();
               BDDHelper db = new BDDHelper();
     
               Habilitation h = null;
               try
               {
                   string query;
                   MySqlConnection cnx = db.open();
                   query = "SELECT ag2_ha_code, ";
                   query += "ag2_ha_Identifiant, ";
                   query += "ag2_ha_Nom, ";
                   query += "ag2_ha_Prenom, ";
                   query += "ag2_ha_MotDePasse, ";
                   query += "ag2_ha_Application, ";
                   query += "ag2_ha_ApplicationLibelle, ";
                   query += "ag2_ha_Profil, ";
                   query += "ag2_ha_Organisation, ";
                   query += "ag2_ha_Perimetre, ";
                   query += "ag2_ha_PerimetreLibelle, ";
                   query += "ag2_ha_Entite, ";
                   query += "ag2_ha_EntiteLibelle, ";
                   query += "ag2_ha_DateHabilitation, ";
                   query += "ag2_ha_Habiliteur, ";
                   query += "ag2_ha_DateSuppression, ";
                   query += "ag2_ha_Resilieur, ";
                   query += "ag2_ha_DerniereHabilitation, ";
                   query += "ag2_ha_OrganisationO, ";
                   query += "ag2_ha_ImpressionLogo ";
                   query += "FROM ag2_habilitations ";
                   query += "WHERE ag2_ha_Identifiant = '";
                   query += cuid;
                   query += "';";
     
     
                   MySqlCommand cmd = new MySqlCommand(query, cnx);
                   MySqlDataReader dr = cmd.ExecuteReader();
     
                   while (dr.Read())
                   {
                       //CODE
                       string code = dr.GetString("ag2_ha_code");
                       //CUID ORANGE
                       string cuidOrange;
                       if (!dr.IsDBNull(1))
                       {
                           cuidOrange = dr.GetString("ag2_ha_Identifiant");
                       }
                       else
                       {
                           cuidOrange = Convert.ToString(null);
                       }
                       //NOM
                       string nom;
                       if (!dr.IsDBNull(2))
                       {
                           nom = dr.GetString("ag2_ha_Nom");
                       }
                       else
                       {
                           nom = Convert.ToString(null);
                       }
                       //PRENOM
                       string prenom;
                       if (!dr.IsDBNull(3))
                       {
                           prenom = dr.GetString("ag2_ha_Prenom");
                       }
                       else
                       {
                           prenom = Convert.ToString(null);
                       }
                       //PASSWORD
                       string password;
                       if (!dr.IsDBNull(4))
                       {
                           password = dr.GetString("ag2_ha_MotDePasse");
                       }
                       else
                       {
                           password = Convert.ToString(null);
                       }
                       //APPLICATION
                       string application;
                       if (!dr.IsDBNull(5))
                       {
                           application = dr.GetString("ag2_ha_Application");
                       }
                       else
                       {
                           application = Convert.ToString(null);
                       }
                       //APLICATION LIBELLE
                       string applicationLibelle;
                       if (!dr.IsDBNull(6))
                       {
                           applicationLibelle = dr.GetString("ag2_ha_ApplicationLibelle");
                       }
                       else
                       {
                           applicationLibelle = Convert.ToString(null);
                       }
                       //PROFIL
                       string profil;
                       if (!dr.IsDBNull(7))
                       {
                           profil = dr.GetString("ag2_ha_Profil");
                       }
                       else
                       {
                           profil = Convert.ToString(null);
                       }
                       //ORGANISATION
                       string organisation;
                       if (!dr.IsDBNull(8))
                       {
                           organisation = dr.GetString("ag2_ha_Organisation");
                       }
                       else
                       {
                           organisation = Convert.ToString(null);
                       }
                       //PERIMETRE
                       string perimetre;
                       if (!dr.IsDBNull(9))
                       {
                           perimetre = dr.GetString("ag2_ha_Perimetre");
                       }
                       else
                       {
                           perimetre = Convert.ToString(null);
                       }
                       //PERIMETRE LIBELLE
                       string perimetreLibelle;
                       if (!dr.IsDBNull(10))
                       {
                           perimetreLibelle = dr.GetString("ag2_ha_PerimetreLibelle");
                       }
                       else
                       {
                           perimetreLibelle = Convert.ToString(null);
                       }
                       //ENTITE
                       string entite;
                       if (!dr.IsDBNull(11))
                       {
                           entite = dr.GetString("ag2_ha_Entite");
                       }
                       else
                       {
                           entite = Convert.ToString(null);
                       }
                       //ENTITE LIBELLE
                       string entiteLibelle;
                       if (!dr.IsDBNull(12))
                       {
                           entiteLibelle = dr.GetString("ag2_ha_EntiteLibelle");
                       }
                       else
                       {
                           entiteLibelle = Convert.ToString(null);
                       }
                       //DATE HABILITATION
                       DateTime dateHabilitation;
                       if (!dr.IsDBNull(13))
                       {
                           dateHabilitation = dr.GetDateTime("ag2_ha_DateHabilitation");
                       }
                       else
                       {
                           dateHabilitation = Convert.ToDateTime(null);
                       }
                       //HABILITEUR
                       string habiliteur;
                       if (!dr.IsDBNull(14))
                       {
                           habiliteur = dr.GetString("ag2_ha_Habiliteur");
                       }
                       else
                       {
                           habiliteur = Convert.ToString(null);
                       }
                       //DATE SUPRESSION
                       DateTime dateSupression;
                       if (!dr.IsDBNull(15))
                       {
                           dateSupression = dr.GetDateTime("ag2_ha_DateSuppression");
                       }
                       else
                       {
                           dateSupression = Convert.ToDateTime(null);
                       }
                       //RESILIEUR
                       string resilieur;
                       if (!dr.IsDBNull(16))
                       {
                           resilieur = dr.GetString("ag2_ha_Resilieur");
                       }
                       else
                       {
                           resilieur = Convert.ToString(null);
                       }
                       //DERNIERE HABILITATION
                       DateTime derniereHabilitation;
                       if (!dr.IsDBNull(17))
                       {
                           derniereHabilitation = dr.GetDateTime("ag2_ha_DerniereHabilitation");
                       }
                       else
                       {
                           derniereHabilitation = Convert.ToDateTime(null);
                       }
                       //ORGANISATION OLD
                       string organisationOld;
                       if (!dr.IsDBNull(18))
                       {
                           organisationOld = dr.GetString("ag2_ha_OrganisationO");
                       }
                       else
                       {
                           organisationOld = Convert.ToString(null);
                       }
     
                       //Appel constructeur Habilitation
                       h = new Habilitation(code, cuidOrange, nom, prenom, password, application, applicationLibelle, profil, organisation, perimetre, perimetreLibelle, entite, entiteLibelle, dateHabilitation, habiliteur, dateSupression, resilieur, derniereHabilitation, organisationOld);
     
                       listHabilitation.Add(h);
     
                   }
     
               }
               catch (MySqlException ex)
               {
                   System.Console.WriteLine(ex.Message);
                   MessageBox.Show(ex.Message);
               }
               finally
               {
                   db.close();
               }
               return listHabilitation;
           }//fin méthode

  2. #2
    Membre expert
    Avatar de GuruuMeditation
    Homme Profil pro
    .Net Architect
    Inscrit en
    Octobre 2010
    Messages
    1 705
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : Belgique

    Informations professionnelles :
    Activité : .Net Architect
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2010
    Messages : 1 705
    Points : 3 570
    Points
    3 570
    Par défaut
    Essaye en enlevant le "elementname=test" dans le binding. Elementname c'est pour dire d'aller chercher le contexte sur un autre element XAML (un autre contrôle, quoi). Vu que tu as mis la liste dans le DataContext, un simple Binding devrait suffire, il va alors prendre ce qu'il y a dans le DataContext

  3. #3
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2012
    Messages
    116
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 116
    Points : 158
    Points
    158
    Par défaut
    Essaye en enlevant le "elementname=test" dans le binding. Elementname c'est pour dire d'aller chercher le contexte sur un autre element XAML (un autre contrôle, quoi). Vu que tu as mis la liste dans le DataContext, un simple Binding devrait suffire, il va alors prendre ce qu'il y a dans le DataContext
    D'autant que si je ne m'abuse, la priorité va au xaml. Donc si un binding est mis en xaml, il ne prendra pas en compte celui fait dans le code.

    Il ne trouve aucun élément test dans ton xaml, donc test est vide, donc data grid vide. Quelque soit le résultat de" test = listeHabilitationCuid.rechercheHabilitation(cuid);"


    En plus de la solution de Guru, Cela peut aussi se faire en donnant un nom à la dataGrid, et ensuite définir son item source dans le code, mais toujours sans la balise binding dans le xaml:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <DataGrid Name:"dgTest" Margin="40" FontSize="15" Grid.Column="2" Grid.RowSpan="2" AlternatingRowBackground="LightBlue">
                       <DataGrid.Columns>
                           <DataGridTextColumn Binding="{Binding code}" x:Name="dataGrid_codehabilitation" Header="Code Habilitation" />
                           <DataGridTextColumn Binding="{Binding nom}" x:Name="dataGrid_nom" Header="Nom"  />
                           <DataGridTextColumn Binding="{Binding prenom}" x:Name="dataGrid_prenom" Header="Prénom"  />
                           <DataGridTextColumn Binding="{Binding applicationLibelle}" x:Name="dataGrid_application" Header="Application" />
                           <DataGridTextColumn Binding="{Binding perimetreLibelle}" x:Name="dataGrid_perimetre" Header="Périmètre" />
                           <DataGridTextColumn Binding="{Binding dateHabilitation}" x:Name="dataGrid_dateHabilitation" Header="Date d'Habilitation" />
                           <DataGridTextColumn Binding="{Binding habiliteur}" x:Name="dataGrid_habiliteur" Header="Habiliteur" />
                       </DataGrid.Columns>
                   </DataGrid>

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    private void Button_Click(object sender, RoutedEventArgs e)
           {
     
     
               test = new Collection<Habilitation>();
     
               HabilitationDao listeHabilitationCuid = new HabilitationDao();
               test = listeHabilitationCuid.rechercheHabilitation(cuid);
     
              dgTest.ItemsSource = test;
     
           }//fin méthode

  4. #4
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2014
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2014
    Messages : 3
    Points : 4
    Points
    4
    Par défaut
    Merci à vous deux, les deux cas fonctionnes ! Ouf enfin, et merci pour les explications je comprend mieux le système de binding .

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

Discussions similaires

  1. besoin d'aide pour mon programme
    Par pouyoudu17 dans le forum Débuter avec Java
    Réponses: 11
    Dernier message: 28/05/2007, 22h18
  2. Besoin d'aide pour mon premier article tex
    Par amel666 dans le forum Erreurs - Avertissements
    Réponses: 2
    Dernier message: 21/02/2007, 13h18
  3. Besoin d'aide pour mon appli
    Par vince351 dans le forum AWT/Swing
    Réponses: 22
    Dernier message: 31/01/2007, 12h26
  4. besoin d'aide pour mon future site
    Par vulquin dans le forum Général Conception Web
    Réponses: 3
    Dernier message: 11/06/2006, 20h33
  5. Besoin d'aide pour mon rpg en javascript
    Par CyberTwister dans le forum Général JavaScript
    Réponses: 9
    Dernier message: 07/04/2006, 13h26

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