Thanks, Al - your suggestion was sensible - ForEach is the obvious glitch I didn't think of. Problem fixed!
(VB uses () not [] for array indices) Your second suggestion is worth a little effort from me, too - someone may want to check on what was deleted (to the Bin). I'm now getting into Virtual Mode - DGV always irritates me with its long delays, unless everything is tweaked (and I forget what's important). _____ Ian Thomas Victoria Park, Western Australia _____ From: [email protected] [mailto:[email protected]] On Behalf Of Al Gonzalez Sent: Sunday, January 08, 2012 12:17 PM To: [email protected] Subject: Re: DataGridView (WinForms) - .RemoveAt Another option is to store off the indices to be deleted in you For Each loop and then loop over the stored values and delete the appropriate row. -----Original Message----- From: Al Gonzalez Sent: 1/7/2012 11:14 PM Not sure if this is your problem, but I've always had issues deleting from a list (Rows) within a For Each that is traversing the list. I usually use a standard for loop that begins with the last index and counts down to the first. Dim nLastRowIndex As Integer = DataGridView1.Rows.Count - 1 Dim nIndex As Integer = 0 For nIndex = nLastRowIndex To 0 Step -1 If DataGridView1.Rows[nIndex].Cells("Select").Value = True Then ' ... Code to Delete the Row End If Next Code is not tested, and my VB.NET is a bit rusty. -----Original Message----- From: Ian Thomas Sent: 1/7/2012 10:36 PM 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 No virus found in this message. Checked by AVG - www.avg.com Version: 2012.0.1901 / Virus Database: 2109/4728 - Release Date: 01/07/12 No virus found in this message. Checked by AVG - www.avg.com Version: 2012.0.1901 / Virus Database: 2109/4728 - Release Date: 01/07/12
