?
Solved

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

Posted on 2009-04-22
9
Medium Priority
?
1,372 Views
Last Modified: 2013-11-27
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

0
Comment
Question by:demoniumz
  • 4
  • 4
9 Comments
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24208334
> 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
 
LVL 75

Expert Comment

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

    row.Cells(6)

is causing the problem.
0
 
LVL 1

Author Comment

by:demoniumz
ID: 24208417
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:demoniumz
ID: 24208473
forget it the all problem is  now resolved but a new problem born :(
0
 
LVL 39

Expert Comment

by:abel
ID: 24208484
> 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
 
LVL 39

Expert Comment

by:abel
ID: 24208500
> 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
 
LVL 1

Author Comment

by:demoniumz
ID: 24208564
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
 
LVL 1

Author Comment

by:demoniumz
ID: 24208791
anyone????????
0
 
LVL 39

Expert Comment

by:abel
ID: 24208866
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question