Specified Argument was out of Range....

Smokeywade

Member
Joined
Sep 11, 2007
Messages
22
Programming Experience
3-5
ERROR - speciied argument was out of range parameter name: 7 is not valid for index.

I'm trying to calculate the time now minus time on a record and place that result in a column I created in the listview. It works fine when I try to place my results in a column thats on the database (which are 0 - 6)
When placing it in the column I created is when I get the parameter error - Code is below what am I missing...this works fine when using a datagrid...

'This is how I'm populating my listview
VB.NET:
DirAdapter.Fill(Mydataset3, "LateOrders_Header_view")

            Dim dt As DataTable = 


Mydataset3.Tables("LateOrders_Header_view")

            Dim drow As DataRow

            lstGrid.BeginUpdate()
            lstGrid.Items.Clear()
            For Each drow In Mydataset3.Tables("LateOrders_Header_view").Rows
                Dim item As ListViewItem = New ListViewItem(drow("order_no").ToString)
                item.SubItems.Add(drow("name").ToString)
                item.SubItems.Add(drow("order_date").ToString)
                item.SubItems.Add(drow("requested_date").ToString)
                item.SubItems.Add(drow("reason").ToString)
                item.SubItems.Add(drow("status").ToString)
                item.SubItems.Add(drow("duedate").ToString)
                lstGrid.Items.Add(item)
            Next

            fncDaysOpen()
            ' fncDaysLate()
            lstGrid.EndUpdate()

'Snippet of how I'm placing the results
VB.NET:
For row = 0 To Mydataset3.Tables(0).Rows.Count - 1
                Dim order_date As String
                order_date = lstGrid.Items(row).SubItems(2).Text
                Dim conv As DateTime
                conv = System.Convert.ToDateTime(order_date)

                Dim d1 As DateTime = conv
                Dim d2 As DateTime = System.DateTime.Today

                Dim subtract, absday
                subtract = 0
                Select Case DatePart("w", d1)
                    Case 1
                        subtract = subtract + 1
                    Case 7
                        subtract = subtract + 2
                End Select

                Select Case DatePart("w", d2)
                    Case 1
                        subtract = subtract + 2
                    Case 7
                        subtract = subtract + 1
                End Select
                absday = DateDiff("d", d1, d2) - 1
                Dim x As String
                 x = absday - CInt(2 * (absday \ 7))      
                'Error occurs here when placing the results

                lstGrid.Items(row).SubItems(7).Text = x

Thanks - this is not the best way to do subtract date Time to get days not including sat and sunday - but its working - if anybody knows a better way - let me know that will be great.
 
Last edited:
SubItems(7) is the problem, you have subitem indexes 0-6 where 0 is the main item (order_no) and 6 is where you placed (duedate).
 
Thanks John for the reply - Your correct that I have 6 columns from the database and I was trying to place my results in column 7. Column 7 is the column I created.

The problem is that listviews can not add columns and rows once they are bound -

So the resoultion for this is creating the column 7 in my select statement - which solved all my headaches - Thanks to another VB.Expert like your self (Plater) provided me with that info....Thanks for the help...
Here is the syntax for it if needed
VB.NET:
"SELECT *, myIDColumn as [Column 7] FROM MYTABLE"
 
You can add both columns to listview and add subitems to existing listviewitems in runtime, for latter you need to get a reference to the listviewitem object and call its SubItems.Add method.
 
Ohhh....can you...
Do you have a snippet explaining how this is done...If you dont have a snippet or if it is too much of a pain dont worry about providing one.

Thanks Again
 
Since you're going to use it anyway, I'd create it when the other subitems is created, just add empty text.
item.SubItems.Add("")
 
Back
Top