fishingman
New member
- Joined
- Oct 12, 2012
- Messages
- 4
- Programming Experience
- 1-3
Hello,
I am developing an application that reads data from an Access database and copying in an excel file through a datagrid to treatment. To do this I used a for loop to copy the datagrid to excel file. My problem is that the loop sometimes hangs when copying and does not complete this task and sometimes it works properly. I used two methods to the task, but I still have this problem.
The codes used are:
Thank you for helping me to understand why the loop stops although I'm sure the number of iterations that I spend for this loop.
I am developing an application that reads data from an Access database and copying in an excel file through a datagrid to treatment. To do this I used a for loop to copy the datagrid to excel file. My problem is that the loop sometimes hangs when copying and does not complete this task and sometimes it works properly. I used two methods to the task, but I still have this problem.
The codes used are:
VB.NET:
Dim cont As String
For i = 2 To lngRowCount + 1
For j = 1 To 10
cont = DataGridView2.Rows(i - 1).Cells(j - 1).Value
If j = 1 Or j = 6 Or j = 8 Or j = 9 Or j = 10 Then
Excel.ActiveWorkbook.ActiveSheet.Cells(i, j).value = "'" + cont
Else
Excel.ActiveWorkbook.ActiveSheet.Cells(i, j).value = cont
End If
Next
Next
VB.NET:
Dim j As Integer
Dim i As Integer
i = 0
For Each myRow2 In DataGridView2.Rows
j = 0
For Each myColumn2 In DataGridView2.Columns
Dim cont As String
cont = DataGridView2.Rows(i).Cells(j).Value
If j = 1 Or j = 6 Or j = 8 Or j = 9 Or j = 10 Then
Excel.ActiveWorkbook.ActiveSheet.Cells(i + 2, j + 1).value = "'" + cont
Else
Excel.ActiveWorkbook.ActiveSheet.Cells(i + 2, j + 1).value = cont
End If
j = j + 1
Next
i = i + 1
Next
Thank you for helping me to understand why the loop stops although I'm sure the number of iterations that I spend for this loop.