I'm stuck on something that must be fairly simple, if not obvious. It's just
a small home project that should have taken an hour or so.
I am writing a simple utility to clean up duplicate files on disk, which
calculates crc32 for all files of a type in a folder and displays the
checksum and some file info in a Windows Forms DataGridView, which has a
checkbox column (DataGridViewCheckBoxColumn).
Files to be deleted are chosen by the user, by inspection (using the
checksum, etc) and deleted to the Recycle Bin with
My.Computer.FileSystem.DeleteFile(FileToDelete,
FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin
(.NET v4.0, Visual Basic).
Under trial & error testing, I can confirm that all chosen files are
deleted (always) - but I am having consistent troubles with clearing the
deleted rows from the DGV. Several items remain, and their checkboxes remain
checked.
A button starts the delete and clear process. I first check that there are
rows (files) marked for deletion -
Public Function CountChecked() As Integer
Dim counter As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
' Named cell 'Select' is a DataGridViewCheckBoxColumn
If row.Cells("Select").Value = True Then
counter = counter + 1
Debug.Print("{0}", row.Cells("FileName").Value)
End If
Next
Return counter
End Function
Then the file deletion process can begin -
Public Function DeleteChecked() As Boolean
Dim FileToDelete As String = ""
Dim rowToDelete As Int32 = vbNull
Dim nDeleted As Integer = 0
Dim nRowsRemoved As Integer = 0
Try
' loop through and delete the physical files
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("Select").Value = True Then '
the column named "Select" is a DataGridViewCheckBoxColumn
'Fully-pathed filename
FileToDelete = CurrentFolder & "\" &
(row.Cells("FileName").Value)
' Delete (move to Recycle Bin)
'My.Computer.FileSystem.DeleteFile(FileToDelete,
FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin) '
Probably too conservative
My.Computer.FileSystem.DeleteFile(FileToDelete,
FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin) '
should be OK
nDeleted = nDeleted + 1
End If
Next
Debug.Print("Deleted {0} files", nDeleted)
' Result = 15
' Remove rows from the DataGridView
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("Select").Value = True Then
nRowsRemoved = nRowsRemoved + 1
rowToDelete = row.Index
Me.DataGridView1.Rows.RemoveAt(rowToDelete)
Me.DataGridView1.Refresh() ' refresh isn't fixing the
discrepancy
End If
Next
Debug.Print("Removed {0} rows from the DataGridView",
nRowsRemoved) ' Result = 10 removed
Return True
Catch ex As Exception
Debug.Print(ex.Message)
End Try
Return False
End Function
I have a folder full of 5000 image files, many similarly named and with
identical checksums, and my trial & error testing with a small subset in the
DGV always results in a discrepancy similar to the above (highlighted). I
can check the test folder and the recycle bin - and restore any of the
files, of course - and always have rows that are not removed from the DGV.
The discrepancy seems to be up to 50% of the checked rows still remaining
(eg, 4 of 7, 10 of 21, 5 of 15).
There are no permissions problems on the folder or files, and no exceptions
appear. Oh, and the DGV is not in virtual mode.
Suggestions?
_____
Ian Thomas
Victoria Park, Western Australia