OVH Cloud OVH Cloud

System.ArgumentOutOfRangeException

1 réponse
Avatar
Xavier
Bonsoir,

j'ai donc une exception "System.ArgumentOutOfRangeException" en executant
mon code.

En fait je récupère les lignes d'un fichier texte et rempli une
DataGridView (3 colonnes) avec ces lignes (1 ligne par Cell).

Quand j'execute le code, la DataGridView s'affiche, et les valeurs dedans
également correctement.

Mais j'ai quand même cette exception qui ressort ...

je précise que je suis très débutant en VB.net ...

Voici le code en question :

-------------------------------------------------------------------------

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("mytest2.txt")
Dim line As String
Dim wnbline As Integer
Dim wnocell As Integer

' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
If line Is Nothing = False Then
If line.StartsWith("1") Then
DataGridView1.Rows.Add()
wnbline = wnbline + 1
wnocell = 1
End If

DataGridView1.Rows(wnbline - 1).Cells(wnocell -
1).Value = line.Substring(1)
wnocell = wnocell + 1
'Console.WriteLine(line)
End If
'Console.WriteLine(line)
Loop Until line Is Nothing
sr.Close()
End Using
Catch Err As Exception
' Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(Err.Message)
End Try

End Sub

-------------------------------------------------------------------------

Qqun pourrait-il m'aider ?

Merci d'avance.

1 réponse

Avatar
Xavier
Inutile, j'ai trouvé.
Dans mon cas il faut tester line <> "" ...