Index was out of range. Must be non-negative and less than the size of the collection.

Hello i  try to  calculate in datagrid view *order form   i have the code  but is give me error

  Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value
            sum += row.Cells(6).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
End Class

-Error-
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

My code order form is below
Public Class Norders
 
    Dim instance As ArgumentOutOfRangeException
 
   
    Private Sub Norders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Orders' table. You can move, or remove it, as needed.
        Me.OrdersTableAdapter.Fill(Me.NicolaouDBDataSet.Orders)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.OrdersDetails' table. You can move, or remove it, as needed.
        Me.OrdersDetailsTableAdapter.Fill(Me.NicolaouDBDataSet.OrdersDetails)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Products' table. You can move, or remove it, as needed.
        Me.ProductsTableAdapter.Fill(Me.NicolaouDBDataSet.Products)
        'TODO: This line of code loads data into the 'NicolaouDBDataSet.Suppliers' table. You can move, or remove it, as needed.
        Me.SuppliersTableAdapter.Fill(Me.NicolaouDBDataSet.Suppliers)
        
    End Sub
 
 
    Private Sub OrdersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersBindingSource.EndEdit()
        Me.OrdersTableAdapter.Update(Me.NicolaouDBDataSet.Orders)
 
    End Sub
 
    Private Sub movenext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movenext.Click
        OrdersBindingSource.MoveNext()
    End Sub
 
    Private Sub moveprevius_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles moveprevius.Click
        OrdersBindingSource.MovePrevious()
    End Sub
 
    Private Sub movelast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movelast.Click
        OrdersBindingSource.MoveLast()
    End Sub
 
    Private Sub movefirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movefirst.Click
        OrdersBindingSource.MoveFirst()
    End Sub
 
    Private Sub addrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addrec.Click
        OrdersBindingSource.AddNew()
 
 
    End Sub
 
    Private Sub delrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delrec.Click
        OrdersBindingSource.RemoveCurrent()
    End Sub
 
    Private Sub OrdersDetailsBindingSourceBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.OrdersDetailsBindingSource.EndEdit()
        Me.OrdersDetailsTableAdapter.Update(Me.NicolaouDBDataSet.OrdersDetails)
 
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Add(dr)
 
    End Sub
 
 
 
    
 
 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bsource As BindingSource = Me.OrdersDetailsDataGridView.DataSource
        Dim ds As DataSet = DirectCast(bsource.DataSource, DataSet)
 
        Dim dr As DataRow = ds.Tables("OrdersDetails").NewRow
        dr("OrderID") = OrderIDTextBox.Text
        dr("ProductID") = ProductIDComboBox.SelectedValue
        dr("UnitPrice") = UnitPriceTextBox.Text
        dr("Quantity") = QuantityTextBox.Text
        dr("productname") = ProductNameTextBox1.Text
 
        ds.Tables("OrdersDetails").Rows.Clear()
    End Sub
 
    Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
        Dim sum As Decimal = -1
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value
            sum += row.Cells(6).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
End Class

Open in new window

LVL 1
demoniumzAsked:
Who is Participating?
 
abelCommented:
> Index was out of range. Must be non-negative and less than the size of the collection.> Parameter name: index

though you don't say on what line the error occurs, it is likely that the following line is offensive:

row.Cells(6).Value = row.Cells(4).Value * row.Cells(5).Value
Note that if there are six cells, the sixth cell is indexed number five. Collections are zero based.

0
 
käµfm³d 👽Commented:
Does each row actually have 7 columns/cells? If not, then I would say that the

    row.Cells(6)

is causing the problem.
0
 
demoniumzAuthor Commented:
abel: corect i forget that start from 0 NOW is work  but the code not appear result to the total row which is the row 5
Private Sub OrdersDetailsDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles OrdersDetailsDataGridView.CellContentClick
        Dim sum As Decimal = 0
        For Each row As DataGridViewRow In OrdersDetailsDataGridView.Rows
            row.Cells(5).Value = row.Cells(3).Value * row.Cells(4).Value
            sum += row.Cells(5).Value
        Next
        Me.TextBox1.Text = sum
    End Sub
End Class

what i need (  i have a datagrid view  iwant to make the calculate  for each row.I  have quantity  and unit price (the logic is to multiply quantity  with the unit price) but i dont know who to write it down to  vb and where.

Also i need to calculate total rows sum ) can u help  because the code i have is not display it

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
demoniumzAuthor Commented:
forget it the all problem is  now resolved but a new problem born :(
0
 
abelCommented:
> but the code not appear result to the total row which is the row 5
 my comment was about the cell, not about the rows You loop through all rows, so that should be ok

> what i need (  i have a datagrid view  iwant to make the calculate  for each row.

That's a totally different question. But to give you a hint: use the DataGridViewCellEventArgs, which contains the current row.

> Also i need to calculate total rows sum ) can u help  because the code i have is not display it
Yet another question. Note that EE is all about one question per thread, to keep it simple for the archive and for experts (you may want to check the guidelines under help). Yet, your code above does that already, I believe (barring a few mistakes depending on the selected rows).

0
 
abelCommented:
> forget it the all problem is  now resolved but a new problem born :(

ok. Just post a new question. You'll have more chance on a quick answer if you post a new question because thousands of experts review the questions asked.
0
 
demoniumzAuthor Commented:
when i add a new product in the datagrid the total  is not appear and the sum not change.2nd problem  when  a product remove it the sum is not change 3rd add a new  order  the total is not reset to  0]

apologize but need soon
0
 
demoniumzAuthor Commented:
anyone????????
0
 
abelCommented:
Like I said, you should really post these new questions as new questions. Repeating them here is not a good idea. And since we're all volunteers with limited time, pushing for rush is not a good idea, usually. Sorry if I seem harsh, but I do suggest you to read the guidelines of EE.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.