6 Replies - 278 Views - Last Post: 01 February 2019 - 10:10 AM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 16-May 17

Showing error :index was out of range. In datagridview

Posted 31 January 2019 - 10:13 PM

hello
sir
getting error when run the loop and going to last value of row count
please help

and i can not use the 2nd loop in the place of cells(?????) number how can i use this

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


Dim aa As Integer
       For Z1 = 0 To DataGridView1.RowCount

           If Me.DataGridView1.Rows(Z1).Cells(2).Value = True Then
               aa = 1
           Else
               aa = 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(3).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(4).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(5).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(6).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
       Next

This post has been edited by andrewsw: 01 February 2019 - 01:20 AM
Reason for edit:: removed "please help..." fluff from title


Is This A Good Question/Topic? 0
  • +

Replies To: Showing error :index was out of range. In datagridview

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14845
  • View blog
  • Posts: 59,246
  • Joined: 12-June 08

Re: Showing error :index was out of range. In datagridview

Posted 31 January 2019 - 10:20 PM

It's any normal out of bounds issue.

Say you have a collection of 3 objects. What are their indexes?

0, 1, 2 - right?

When you loop goes to collection's count, 3 in this case, you are trying to access something at index 3.. which doesn't exist. Hence out of bounds.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 16-May 17

Re: Showing error :index was out of range. In datagridview

Posted 31 January 2019 - 10:27 PM

View Postmodi123_1, on 01 February 2019 - 10:50 AM, said:

It's any normal out of bounds issue.

Say you have a collection of 3 objects. What are their indexes?

0, 1, 2 - right?

When you loop goes to collection's count, 3 in this case, you are trying to access something at index 3.. which doesn't exist. Hence out of bounds.


yes sir I have 39 Rows when go to Z1=39 number then show the problem (and column =0 to 7 means eight column)
please help me sir how to solve this problem
please help me sir I 1st time use datagridview
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14845
  • View blog
  • Posts: 59,246
  • Joined: 12-June 08

Re: Showing error :index was out of range. In datagridview

Posted 31 January 2019 - 10:29 PM

Again.. it's the same thing. If you have 39 rows what's the max index number in the collection.. 38. So make your loop go from zero to one minus the total count.
Was This Post Helpful? 0
  • +
  • -

#5 [email protected]   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 16-May 17

Re: Showing error :index was out of range. In datagridview

Posted 31 January 2019 - 10:38 PM

View Postmodi123_1, on 01 February 2019 - 10:59 AM, said:

Again.. it's the same thing. If you have 39 rows what's the max index number in the collection.. 38. So make your loop go from zero to one minus the total count.


Sir, I am sorry, please forgive me
Can you tell me the answer how to solve it.
Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 368
  • View blog
  • Posts: 1,577
  • Joined: 07-September 13

Re: Showing error :index was out of range. In datagridview

Posted 01 February 2019 - 01:59 AM

He has told you.

Look at your for the statement, it goes 0 to rows.count.

You have 39 rows (that's the count) which are indexed as 0-38.

So instead of going 0 to row.count(39) you want to 0 to 38 (count - 1)
Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 198
  • View blog
  • Posts: 1,115
  • Joined: 05-December 13

Re: Showing error :index was out of range. In datagridview

Posted 01 February 2019 - 10:10 AM

Your indexes begin at zero, and not one. So if you actually have 39 apples in a bowl, recount them from zero to the last apple, and how many do you have left if you count from zero and not one? What is the last number? Now that you know that, what do you think you need to do when looping? If you still don't get it, have read over collections on MSDN or the error you are getting https://docs.microso...framework-4.7.2

Consider the first apple you count is not one, but zero. 0, 1, 2, 3, 4 etc ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1