Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview in form2 . I put in 10 columns with columns width
programmatically. Data are copied from Datagridview contained in form1 to datagridview in form2.When I try to cumulate the total width of the 10 columns in form2 , I use the Do while ...Loop block. Running through the code in the block goes normally, but the program stops working instead of continuing to run the other code after the Do While...Loop . usually the same code worked correctly . I don't understand the reason of that strange behavior. It is as if the program attempt to go beyond the tenth column and stops working sending the message:index was out of range, must be non negative and less than the size of the collection.Parameters name: index .If I change something in the Do while loop block as putting for example Do While i < 5 ....loop , when running ,the program reaches 4 and tries to go beyond to 5 and stops working with the message index was out of range....... Why the program is attempting go beyond the limit indicated (limit <5 or limit <=datagridview1.rowcount-1 in my code ) ? I need help my dear friends

What I have tried:

VB.NET
'To add columns and columns widths to Datagridview1 OF FORM2
 Private Sub form2_Load(sender As Object, e As EventArgs) Handles Me.Load
       
            DataGridView1.Columns.Add("cpteprinci", "No Compte")
            DataGridView1.Columns.Add("journgencd", "Intitule")
            DataGridView1.Columns.Add("TotalDb", "Ouverture Debits")
            DataGridView1.Columns.Add("Totalcd", "Ouverture Credits")
            DataGridView1.Columns.Add("column5", "Mouvements Debiteurs")
            DataGridView1.Columns.Add("column6", "Mouvements Crediteurs")
            DataGridView1.Columns.Add("column7", "Total Debits")
            DataGridView1.Columns.Add("column8", "Total Credits")
            DataGridView1.Columns.Add("column9", "Soldes Debiteurs")
            DataGridView1.Columns.Add("column10", "Soldes Crediteurs")


            DataGridView1.Columns(0).Width = 60
            DataGridView1.Columns(1).Width = 140
            DataGridView1.Columns(2).Width = 75
            DataGridView1.Columns(3).Width = 75
            DataGridView1.Columns(4).Width = 75
            DataGridView1.Columns(5).Width = 75
            DataGridView1.Columns(6).Width = 75
            DataGridView1.Columns(7).Width = 75
            DataGridView1.Columns(8).Width = 75
            DataGridView1.Columns(9).Width = 75

'To transfer data from form1 to form2

Dim n As Integer = 0
        For Each row As DataGridViewRow In form1.DataGridView1.Rows
            If (Form1.DataGridView1.Rows.Count <> (n + 1)) Then
                DataGridView1.Rows.Add()
                For p As Integer = 0 To Form1.DataGridView1.ColumnCount - 1
                    DataGridView1.Rows(n).Cells(p).Value = row.Cells(p).Value
                Next
            End If

            n = n + 1
        Next
end sub

'To calculate the cumulative of all columns
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 Dim wdt As Integer = 0
dim arrcol as new arraylist
dim ik as integer=25


dim i as integer=0


Do While i <=  DataGridView1.ColumnCount - 1
                        wdt = DataGridView1.Columns(i).width 
                     
                         ik =ik+ wdt
                        arrcol.Add(ik)
                        i = i+ 1
                        
 
 loop                     
Posted
Updated 23-Jul-21 19:20pm
v2
Comments
Richard MacCutchan 24-Jul-21 4:18am    
Using the name DataGridView1 on both forms does not make your code easy to read or understand.
Member 14904307 24-Jul-21 15:47pm    
Dear kreskowiak, I have changed form1.datagridview1 to TrsfDgv1 but the problems persists. There is any problem when transferring data from TrsfDgv1 in form1 to datagridview1 in form2. The problem occurs when running the block Do while ...loop in the last part of the code
Member 14904307 24-Jul-21 15:49pm    
Dear Richard, I have changed form1.datagridview1 to TrsfDgv1 but the problems persists. There is any problem when transferring data from TrsfDgv1 in form1 to datagridview1 in form2. The problem occurs when running the block Do while ...loop in the last part of the code

1 solution

We can't run your code to test it - we have no access to the data from which it feeds in your DataGridView. And you need the code running with your data to stand a chance of working out what is going on.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 14904307 24-Jul-21 5:10am    
thank you for your advise, i am going to understand about Visual studio debugger and i will tell you after. thanks
OriginalGriff 24-Jul-21 7:22am    
You're welcome!
Member 14904307 24-Jul-21 17:03pm    
Dear OriginalGriff, I have used the vs debugger and I found that when the variable i (see in the do while loop block) reached the value 9, the iteration continued instead of to exit the do while loop block as indicated in the block. The variable i continues to iterate ,taking the value 10 that is greater than 9 (limit of the iteration allowed by the block). but why the variable don't stop at the allowed limit of 9 (datagridview1.columncount-1)
OriginalGriff 24-Jul-21 17:10pm    
Because the column count is not what you think it is.
What did the debugger show you it's value was?
Member 14904307 24-Jul-21 17:16pm    
note that when the variable i exceeds the value 9 and takes value 10, the program stops and the message index was out of range.... occurs.Normally the variable i should not exceed the value 9.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900