0

I have the following code: public string selectS() {

        int NbrLingneSelect = 0;
        for (int i = 0; i < VCL.Rows.Count; i++)
        {
            if ((bool)VCL.Rows[i].Cells[0].Value == true)
            {
              NbrLingneSelect++;
            }
            
        }
        
        if (NbrLingneSelect == 0)
        {
           return "Selectionner une Véhicule  ";
        }
        if (NbrLingneSelect > 1)
        {
            return "Selectionner  une seul Véhicule ";
        }

        return null;
    }

it's calls exception System.NullReferenceException : 'La référence d'objet n'est pas définie à une instance d'un objet.'

System.Windows.Forms.DataGridViewCell.Value.get retournée null.

  • You need to make sure `VCL.Rows[i].Cells[0].Value` is not `null` “BEFORE” you try to compare its value. `If (VCL.Rows[i].Cells[0].Value != null) {…}` If it is null, and it is a check box cell… then null means its not checked i.e. false. – JohnG Aug 10 '20 at 22:59
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – JohnG Aug 11 '20 at 00:17

0 Answers0