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 :

Erreur system.nullReferenceException dataGridView


Sujet :

C#

  1. #1
    Nouveau membre du Club
    Inscrit en
    Mai 2010
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Mai 2010
    Messages : 22
    Points : 28
    Points
    28
    Par défaut Erreur system.nullReferenceException dataGridView
    Bonjour,
    chers developpeurs c# je suis plante sur ce probleme depuis pret d'une semaine apres avoir fait le tour des forum je n'ai vraiment vu une discution qui traite mon probleme. En fait j'ai cette erreur
    system.nullreferenceException (la reference d'objet n'est pas definie a une instance).
    Cela se produit lorsque je clique sur le contenu d'une cellule de mon dataGridView. Voici mon code complet:

    Code C# : 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
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
     using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Gesbi.Data;
    using Gesbi.BD;
    using Gesbi.Controleur;
    
    namespace Gesbi.Ihm
    {
    public partial class LivraisonCmdeFournisseurIhm : Form
    {
             private List<CmdeFournisseurData> listCmdeNonLivrer = new List<CmdeFournisseurData>();
             private List<PasserCmdeFournisseurData> listProCmder = new  List<PasserCmdeFournisseurData>();
             private List<FournisseurData> listFournisseur = new List<FournisseurData>();
             private List<ProduitLivrerData> livraisonPartielle = new List<ProduitLivrerData>();
             private double montantLivrerCmdde = 0;
             private double montantLivrerCmddePro = 0;
             private string voirSiCheckBoxValider = "";
             private int QteCondiDjaLivrer = 0;
             private int QteProDjaLivrer = 0;
    
    public LivraisonCmdeFournisseurIhm()
    {
                InitializeComponent();
                chargerComboFourni(FournisseurBD.afficheFournisseur());
    }
    private void chargerCombo(List<CmdeFournisseurData> listCmde)
    {
               this.listCmdeNonLivrer = listCmde;
               comboBoxCmdeAconfirmer.Items.Clear();
               comboBoxCmdeAconfirmer.Items.Add("Choisir la commande");
            for (int i = 0; i < listCmdeNonLivrer.Count; i++)
               {
        comboBoxCmdeAconfirmer.Items.Add(listCmdeNonLivrer[i].getDateCmdeFourni());
                }
                       comboBoxCmdeAconfirmer.SelectedIndex = 0;
    }
    private void chargerComboFourni(List<FournisseurData> listFournisseur)
    {
                 this.listFournisseur = listFournisseur;
                 comboBoxFourni.Items.Clear();
                 comboBoxFourni.Items.Add("Choisir le fournisseur");
             for (int i = 0; i < listFournisseur.Count; i++)
    {
    comboBoxFourni.Items.Add(listFournisseur[i].getDenominationFourni());
    }
    comboBoxFourni.SelectedIndex = 0;
    }
    private void chargerTablo(List<PasserCmdeFournisseurData> listProCmde)
    {
    this.listProCmder = listProCmde;
    dataGridViewCmdeFinal.EndEdit();
    dataGridViewCmdeFinal.Rows.Clear();
    for (int i = 0; i < listProCmder.Count; i++)
    {
    dataGridViewCmdeFinal.Rows.Add();
    dataGridViewCmdeFinal.Rows[i].Cells[0].Value = listProCmder[i].getConcerneProModel().getCodPro().getLibPro() + "(" + listProCmder[i].getConcerneProModel().getCodModel().getLibModel() + ")";
    dataGridViewCmdeFinal.Rows[i].Cells[1].Value = listProCmder[i].getQteCmderCondi();
    dataGridViewCmdeFinal.Rows[i].Cells[2].Value = listProCmder[i].getMontantCmderCondi();
    dataGridViewCmdeFinal.Rows[i].Cells[5].Value = listProCmder[i].getQteCmderPro();
    dataGridViewCmdeFinal.Rows[i].Cells[6].Value = listProCmder[i].getMontantCmderPro();
    }
    dataGridViewCmdeFinal.EndEdit();
    }
    private int rechercheValeurList(List<PasserCmdeFournisseurData> listProCmder, string ch)
    {
    int tr = 0;
    for (int i = 0; i < listProCmder.Count; i++)
    {
    if (listProCmder[i].getNumCondi().getLibCondi().Equals(ch))
    {
    tr = i;
    break;
    }
    else { continue; }
    }
    return tr;
    }
    private double calculMontantLivrer(DataGridView tablo)
    {
    double montantTotal=0;
    for(int i=0;i<tablo.RowCount;i++){
    try
    {
    montantTotal = montantTotal + double.Parse(tablo.Rows[i].Cells[4].Value.ToString()) + double.Parse(tablo.Rows[i].Cells[8].Value.ToString());
    }
    catch (Exception ex)
    {
    Console.Out.WriteLine(ex.Message);
    montantTotal += 0;
    //MessageBox.Show(this, "Une erreur de convertion d'une chaîne en réel s'est produite!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }
    return montantTotal;
    }
    private void button6_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    private void comboBoxCmdeAconfirmer_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBoxCmdeAconfirmer.SelectedIndex > 0)
    {
    chargerTablo(CmdeFournisseurBD.afficheCmdeNonLivrerAconfirmer(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni()));
    dataGridViewCmdeFinal.ReadOnly = false;
    dataGridViewCmdeFinal.Columns[0].ReadOnly = true;
    dataGridViewCmdeFinal.Columns[1].ReadOnly = true;
    dataGridViewCmdeFinal.Columns[2].ReadOnly = true;
    dataGridViewCmdeFinal.Columns[5].ReadOnly = true;
    dataGridViewCmdeFinal.Columns[6].ReadOnly = true;
    dataGridViewCmdeFinal.Columns[8].ReadOnly = true;
    }
    else
    {
    dataGridViewCmdeFinal.Rows.Clear();
    }
    dataGridViewCmdeFinal.EndEdit();
    }
    private void LivraisonCmdeFournisseurIhm_Load(object sender, EventArgs e)
    {
    }
    private void dataGridViewCmdeFinal_CellContentClick(object sender, DataGridViewCellEventArgs e1)
    {
    if (dataGridViewCmdeFinal.RowCount > 0)
    {
    //dataGridViewCmdeFinal.EndEdit();
    
    // c'est ici que l'exception se signale
    voirSiCheckBoxValider = dataGridViewCmdeFinal[9, e1.RowIndex].Value.ToString();
    if (voirSiCheckBoxValider.Equals("True"))
    {
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].ReadOnly = true;
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].Value = "" + (int.Parse(dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[1].Value.ToString()) - QteCondiDjaLivrer);
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].ReadOnly = true;
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].Value = "" + (int.Parse(dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[5].Value.ToString()) - QteProDjaLivrer);
    }
    else
    {
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].ReadOnly = false;
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[3].Value = "";
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[4].Value = "";
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].ReadOnly = false;
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[7].Value = "";
    dataGridViewCmdeFinal.Rows[e1.RowIndex].Cells[8].Value = "";
    }
    livraisonPartielle = LivraisonCmdeFourniBD.afficheLivraisonPartiel(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni(), listProCmder[e1.RowIndex].getConcerneProModel().getCodPro().getCodPro(), listProCmder[e1.RowIndex].getConcerneProModel().getCodModel().getCodModel());
    if (livraisonPartielle.Count > 0)
    {
    textBoxQteCondiDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerCondi();
    textBoxQteProDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerPro();
    QteCondiDjaLivrer = livraisonPartielle[0].getQteLivrerCondi();
    QteProDjaLivrer = livraisonPartielle[0].getQteLivrerPro();
    }
    else
    {
    textBoxQteCondiDjaLivrer.Text = "" + 0;
    textBoxQteProDjaLivrer.Text = "" + 0;
    QteCondiDjaLivrer = 0;
    QteProDjaLivrer = 0;
    }
    }
    else
    {
    textBoxQteCondiDjaLivrer.Text = "";
    textBoxQteProDjaLivrer.Text = "";
    QteCondiDjaLivrer = 0;
    QteProDjaLivrer = 0;
    }
    dataGridViewCmdeFinal.EndEdit();
    }
    private void dataGridViewCmdeFinal_CellValueChanged(object sender, DataGridViewCellEventArgs e2)
    {
    dataGridViewCmdeFinal.EndEdit();
    if (dataGridViewCmdeFinal.RowCount > 0)
    {
    int qteLivrerCom=0,qteLivrerPro=0,qteCmderPro=0,qteCmderCom=0;
    try
    {
    qteCmderCom = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[1].Value.ToString());
    if (qteCmderCom == 0)
    {
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + 0;
    }
    else
    {
    qteLivrerCom = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value.ToString());
    int sommeQteAlivrerEtLivrerCondi = qteLivrerCom + QteCondiDjaLivrer;
    if (sommeQteAlivrerEtLivrerCondi <= qteCmderCom)
    {
    montantLivrerCmdde = qteLivrerCom * (double.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[2].Value.ToString()) / int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[1].Value.ToString()));
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + montantLivrerCmdde;
    }
    else
    {
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[3].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[4].Value = "" + 0;
    }
    }
    qteCmderPro = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[5].Value.ToString());
    if (qteCmderPro == 0)
    {
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + 0;
    }
    else
    {
    qteLivrerPro = int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value.ToString());
    int sommeQteAlivrerEtLivrerPro = qteLivrerPro + QteProDjaLivrer;
    if (sommeQteAlivrerEtLivrerPro <= qteCmderPro)
    {
    montantLivrerCmddePro = qteLivrerPro * (double.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[6].Value.ToString()) / int.Parse(dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[5].Value.ToString()));
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + montantLivrerCmddePro;
    }
    else
    {
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[7].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e2.RowIndex].Cells[8].Value = "" + 0;
    }
    }
    }
    catch (Exception ex)
    {
    Console.Out.WriteLine(ex.Message);
    //MessageBox.Show(this, "Vérifiez que vous n'avez pas saisie une chaîne au lieu d'un chiffre réel sur la colonne quantitée livrée!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    if (qteLivrerCom != 0 || qteLivrerPro != 0)
    {
    buttonEnregistrer.Enabled = true;
    }
    else
    {
    buttonEnregistrer.Enabled = false;
    }
    }
    }
    private void buttonEnregistrer_Click(object sender, EventArgs e)
    {
    LivraisonCmdeData cmdeLivrer = new LivraisonCmdeData(0, listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1], MaterielControl.dateDuJr(), calculMontantLivrer(dataGridViewCmdeFinal));
    List<ProduitLivrerData> livraisonTotal=new List<ProduitLivrerData>();
    //MessageBox.Show(""+cmdeLivrer);
    int qteLivrerPro = 0, qteLivrerCondi = 0;
    double montantLivrerPro = 0, montantLivrerCondi = 0;
    for (int i = 0; i < dataGridViewCmdeFinal.RowCount; i++)
    {
    try
    {
    qteLivrerPro = int.Parse(dataGridViewCmdeFinal.Rows[i].Cells[7].Value.ToString());
    montantLivrerPro = double.Parse(dataGridViewCmdeFinal.Rows[i].Cells[8].Value.ToString());
    qteLivrerCondi = int.Parse(dataGridViewCmdeFinal.Rows[i].Cells[3].Value.ToString());
    montantLivrerCondi = double.Parse(dataGridViewCmdeFinal.Rows[i].Cells[4].Value.ToString());
    ProduitData produit = new ProduitData(listProCmder[i].getConcerneProModel().getCodPro().getCodPro(), listProCmder[i].getConcerneProModel().getCodPro().getLibPro(), null);
    ModelProduitData model = new ModelProduitData(listProCmder[i].getConcerneProModel().getCodModel().getCodModel(), listProCmder[i].getConcerneProModel().getCodModel().getLibModel(), "");
    ConcernerData concerner = new ConcernerData(produit, model);
    livraisonTotal.Add(new ProduitLivrerData(cmdeLivrer, concerner, listProCmder[i].getNumCondi(), qteLivrerPro, qteLivrerCondi, montantLivrerPro, montantLivrerCondi));
    }
    catch (Exception ex) { Console.Out.WriteLine(ex.Message); }
    }
    if (calculMontantLivrer(dataGridViewCmdeFinal) != 0)
    {
    if (LivraisonCmdeFourniBD.enregistrerLivraisonCmde(livraisonTotal) == true)
    {
    MessageBox.Show(this, "La confirmation de votre livraison s'est effectuée avec succès!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
    MessageBox.Show(this, "Une erreur est survenue lors de la confirmation de cette livraison!", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }
    }
    private void dataGridViewCmdeFinal_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
    }
    private void comboBoxFourni_SelectedIndexChanged(object sender, EventArgs e)
    {
    if (comboBoxFourni.SelectedIndex > 0)
    {
    comboBoxCmdeAconfirmer.Enabled = true;
    chargerCombo(CmdeFournisseurBD.afficheCmdeNonLivrer(listFournisseur[comboBoxFourni.SelectedIndex - 1].getCodFourni()));
    //comboBoxCmdeAconfirmer.SelectedIndex = 1;
    }
    else
    {
    comboBoxCmdeAconfirmer.Items.Clear();
    dataGridViewCmdeFinal.Rows.Clear();
    comboBoxCmdeAconfirmer.Enabled = false;
    }
    }
    private void dataGridViewCmdeFinal_CellClick(object sender, DataGridViewCellEventArgs e3)
    {
    if (dataGridViewCmdeFinal.RowCount > 0)
    {
    livraisonPartielle = LivraisonCmdeFourniBD.afficheLivraisonPartiel(listCmdeNonLivrer[comboBoxCmdeAconfirmer.SelectedIndex - 1].getNumCmdeFourni(), listProCmder[e3.RowIndex].getConcerneProModel().getCodPro().getCodPro(), listProCmder[e3.RowIndex].getConcerneProModel().getCodModel().getCodModel());
    if (livraisonPartielle.Count > 0)
    {
    textBoxQteCondiDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerCondi();
    textBoxQteProDjaLivrer.Text = "" + livraisonPartielle[0].getQteLivrerPro();
    QteCondiDjaLivrer = livraisonPartielle[0].getQteLivrerCondi();
    QteProDjaLivrer = livraisonPartielle[0].getQteLivrerPro();
    try
    {
    int condiCmder = int.Parse(dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[1].Value.ToString());
    int proCmder = int.Parse(dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[5].Value.ToString());
    if (QteCondiDjaLivrer == condiCmder)
    {
    dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[3].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[3].ReadOnly = true;
    }
    if (QteProDjaLivrer == proCmder)
    {
    dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[7].Value = "" + 0;
    dataGridViewCmdeFinal.Rows[e3.RowIndex].Cells[7].ReadOnly = true;
    }
    }
    catch (Exception ex)
    {
    Console.Out.WriteLine(ex.Message);
    }
    }
    else
    {
    textBoxQteCondiDjaLivrer.Text = "" + 0;
    textBoxQteProDjaLivrer.Text = "" + 0;
    QteCondiDjaLivrer = 0;
    QteProDjaLivrer = 0;
    }
    }
    else
    {
    textBoxQteCondiDjaLivrer.Text = "";
    textBoxQteProDjaLivrer.Text = "";
    QteCondiDjaLivrer = 0;
    QteProDjaLivrer = 0;
    }
    dataGridViewCmdeFinal.EndEdit();
    }
    }
    }

  2. #2
    Expert confirmé

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2010
    Messages
    2 066
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2010
    Messages : 2 066
    Points : 4 233
    Points
    4 233
    Par défaut
    tu peux mettre ton code entre les balises es-tu sur que ton datagrid a 10 colonnes (un tableau commence à partir de0 donc 9 est le 10ème élément du tableau)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    voirSiCheckBoxValider = dataGridViewCmdeFinal[9, e1.RowIndex].Value.ToString();
    tu peux vérifier si l'élément est null avant d'en récupérer sa valeur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    if(dataGridViewCmdeFinal[9, e1.RowIndex]!=null && dataGridViewCmdeFinal[9, e1.RowIndex].Value!=null)
    {
    voirSiCheckBoxValider = dataGridViewCmdeFinal[9, e1.RowIndex].Value.ToString();

Discussions similaires

  1. Réponses: 12
    Dernier message: 14/01/2019, 01h12
  2. [Débutant] Erreur De :System.NullReferenceException
    Par Taralali dans le forum ASP.NET
    Réponses: 1
    Dernier message: 05/03/2012, 18h18
  3. Réponses: 5
    Dernier message: 26/04/2010, 10h52
  4. Erreur System.NullReferenceException avec MySQL
    Par CleeM dans le forum Windows Forms
    Réponses: 4
    Dernier message: 26/02/2008, 17h17
  5. Réponses: 2
    Dernier message: 29/01/2008, 12h04

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