1

Additional information: Index -1 does not have a value.

I am creating data gestion program this form3 contains a DGV shows data from my MS Access DB and another DGV1 contains items from a list of objects.

The problem is when I click on any item of DGV1 the error shows and the program break

I tried it with another program and it works just fine
I will past the code of form here

Imports System.IO
Imports Newtonsoft.Json

Public Class Form3
    Public Shared d As devis = New devis() 'creatig devis

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataATDataSet.DataArticl' table. You can move, or remove it, as needed.
        Me.DataArticlTableAdapter.Fill(Me.DataATDataSet.DataArticl) 'fill DGV with data in db 

        devisBindingSource.DataSource = d.listArticle 'DGV1 data source list of article in devis d

        DataGridView1.DataSource = devisBindingSource
    End Sub

    Private sRef As String          'variable to put selected row values 
    Private sNomArticle As String
    Private sPU As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sRef = DataArticlDataGridView.CurrentRow.Cells(0).Value
        sNomArticle = DataArticlDataGridView.CurrentRow.Cells(1).Value
        sPU = DataArticlDataGridView.CurrentRow.Cells(2).Value
        d.addArticle(sRef, sNomArticle, CDbl(sPU), CDbl(TextBox1.Text)) 'adding article to list
        DataGridView1.DataSource = Nothing                      'refreshing data source
        DataGridView1.DataSource = devisBindingSource
        DataGridView1.Refresh()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim savePath As String = Directory.GetCurrentDirectory + "/projects/" + Form1.projectName  'saving folder
        File.WriteAllText(savePath + "/devis.json", JsonConvert.SerializeObject(d))   'serialize object in json
        MsgBox("Saved")
    End Sub

End Class
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Your button click assumes a CurrentRow isn't null. Which line actually causes the exception? – LarsTech Mar 30 '19 at 20:48
  • @LarsTech my button click is working fine with the good DGV the exception is when i click on any items of DataGridView1 – AlgerianCoder19 Mar 30 '19 at 21:16
  • When you say "when I click on any item of DGV1" do you mean click on a cell in the DataGridView? Or click on one of the buttons (which one?). – GregHNZ Mar 30 '19 at 21:16
  • @KenWhite i guess no in the thread u mentionned he didn't talk about datagridV click i am really confused – AlgerianCoder19 Mar 30 '19 at 21:16
  • We aren't looking at the code that is causing your error. Do you have anything in the grid's events that you are using that throws the exception. Again, which line throws the error? Until we see the line of code that causes the error, we can't do much. – LarsTech Mar 30 '19 at 21:21
  • It's the same exception, caused by the same thing (accessing items outside the array or collection bounds), and solved the same way. It's a duplicate. – Ken White Mar 30 '19 at 21:23

0 Answers0