Buongiorno a tutti
VB.Net Visual studio 2022
Ho una Datagridview non collegata ad un database, riempita con un file .txt
per la ricerca ho scritto questa function:
Private Function RicercaLoc(Loc As String) As Boolean
Try
For Each row As DataGridViewRow In DGVLocaz.Rows
For Each Cell As DataGridViewCell In row.Cells
If Cell.Value.ToString = Loc Then
RicercaLoc = True
Return RicercaLoc
Exit Function
End If
Next
Next
Catch ex As Exception
RicercaLoc = False
End Try
Return RicercaLoc
End Function
Il tutto funziona, ma scorre tutte le celle. Siccome so quale è la colonna che contiene il codice da ricercare ho provato a modificare in questa maniera
togliendo il secondo FOR
Private Function RicercaLoc(Loc As String) As Boolean
Try
For Each row As DataGridViewRow In DGVLocaz.Rows
If row.Cells(0).Value.ToString = Loc Then
RicercaLoc = True
Return RicercaLoc
Exit Function
End If
Next
Catch ex As Exception
RicercaLoc = False
End Try
Return RicercaLoc
End Function
In compilazione non mi da errore ma in esecuzione segnala errore
System.NullReferenceException: 'Riferimento a un oggetto non impostato su un'istanza di oggetto.'
System.Windows.Forms.DataGridViewCell.Value.get ha restituito Nothing.
Cosa ho sbagliato?
Franco