Question datagridview + download progress

featrick

New member
Joined
Nov 16, 2011
Messages
4
Programming Experience
Beginner
hi, i try download each link from datagridview.. but the code i wrote its just like multiple download.. even, the progressbar just function at 1st file download and then, didnt work anymore.. so, is anyone can help me how do i write the code to make my program downloading file 1 by 1 with progressbar(current and overall progress)?

here my interface..
asdasd.jpg
 
If you don't show us the code you wrote then we can't tell you how to fix it.

How you would display progress depends on what progress you want to show. If it's number of files then that's easy but if it's amount of data then that's not so easy for overall progress because you'd have to get the size of all files to start with.
 
If you don't show us the code you wrote then we can't tell you how to fix it.

How you would display progress depends on what progress you want to show. If it's number of files then that's easy but if it's amount of data then that's not so easy for overall progress because you'd have to get the size of all files to start with.

just picture at my post is just for example situation.. but now, i will show u the picture of real situation with code on Fix OJN button..

repair.jpg

here my code on download file button..


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim
L As Integer For L = 0 To DataGridView1.RowCount - 1
'get ID

Dim ID = DataGridView1.Item(0, L).Value
'get status file

Dim
state = DataGridView1.Item(2, L).Value
If state = "OK" Then
'status OK = do nothing
ElseIf state = "ERROR" Then
'ERROR and will download fixed file
DataGridView1.Item(2, L).Value = CStr("FIXING")
Dim
filename = "o2ma" & ID & ".rar"
Dim linkfile As New Uri("http://www.ujd.x10.bz/OJNRepaired/" & filename)
Dim locate = System.Environment.CurrentDirectory & "\update\" & filename
Dim link As New Uri(linkfile) client.DownloadFileAsync(link, locate)
End If
Next

End
Sub
 
why i always got error like this "Exception has been thrown by the target of an invocation.".. its didnt show which part code that got error occur..

here my code..
Class WC
Inherits WebClient
Friend Address As Uri
Friend FileName As String
Friend Progress As Integer
End
Class

Dim
WCs As New List(Of WC)

Private
Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For
L As Integer = 0 To DataGridView2.RowCount - 1
Dim
S(3) As String
For
M As Integer = 0 To S.Length - 1
S(M) = DataGridView2.Item(M, L).Value.ToString
Next
If
Not S(2) = "ERROR" Then Continue For
Button1.Enabled = False
WCs.Add(New WC)
AddHandler WCs(L).DownloadProgressChanged, AddressOf WCs_ProgressChanged
AddHandler
WCs(L).DownloadFileCompleted, AddressOf WCs_DownloadCompleted
WCs(L).Address = New Uri(S(3))
WCs(L).FileName = S(0)
Dim D As String = CurDir() & "\update"
If Not Directory.Exists(D) Then Directory.CreateDirectory(D)
D = Path.Combine(D, S(0) & ".rar")
WCs(L).DownloadFileAsync(WCs(L).Address, D)
Next
End
Sub

Sub WCs_ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
DirectCast
(sender, WC).Progress = e.ProgressPercentage
ProgressBar1.Value = 0
For
Each WC As WC In WCs
ProgressBar1.Value += WC.Progress
Next

End Sub

Sub
WCs_DownloadCompleted(sender As Object, e As AsyncCompletedEventArgs)
Dim
WC As WC = DirectCast(sender, WC)
Dim Msg As String
If e.Error Is Nothing Then
Msg = WC.FileName + " downloaded sucessfully"
Else
Msg = "Error downloading " + WC.FileName
End
If
WCs.Remove(WC) WC.Dispose()
If WCs.Count = 0 Then
Button1.Enabled = True
MessageBox.Show("Download Completed")
End
If
End Sub
 
Your code is too hard to read as is. Please post it in
 tags with no colour coding.
 
Dim WCs As New List(Of WC)
  Dim WCSCount As Integer

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For L As Integer = 0 To DataGridView2.RowCount - 1
      Dim S(3) As String
      For M As Integer = 0 To S.Length - 1
        S(M) = DataGridView2.Item(M, L).Value.ToString
      Next
      If Not S(2) = "ERROR" Then Continue For
      Button1.Enabled = False
      WCs.Add(New WC)
      AddHandler WCs(L).DownloadProgressChanged, AddressOf WCs_ProgressChanged
      AddHandler WCs(L).DownloadFileCompleted, AddressOf WCs_DownloadCompleted
      WCs(L).Address = New Uri(S(3))
      WCs(L).FileName = S(0)
      Dim D As String = CurDir() & "\update"
      If Not Directory.Exists(D) Then Directory.CreateDirectory(D)
      D = Path.Combine(D, S(0) & ".rar")
      WCs(L).DownloadFileAsync(WCs(L).Address, D)
      WCSCount += 1
    Next
  End Sub

  Sub WCs_ProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs)
    DirectCast(sender, WC).Progress = e.ProgressPercentage
    ProgressBar1.Value = 0
    For Each WC As WC In WCs
      ProgressBar1.Value += WC.Progress
    Next
    ProgressBar1.Value \= WCSCount
  End Sub

  Sub WCs_DownloadCompleted(sender As Object, e As AsyncCompletedEventArgs)
    Dim WC As WC = DirectCast(sender, WC)
    Dim Msg As String
    If e.Error Is Nothing Then
      Msg = WC.FileName + " downloaded sucessfully"
    Else
      Msg = "Error downloading " + WC.FileName
    End If
    WCs.Remove(WC)
    WC.Dispose()
    If WCs.Count = 0 Then
      Button1.Enabled = True
      MessageBox.Show("Download Completed")
    End If
  End Sub



i hope u can read this one..
 
Back
Top