Results 1 to 2 of 2

Thread: DataGridView Error

  1. #1

    Thread Starter
    New Member Erlkoenig's Avatar
    Join Date
    Mar 2024

    DataGridView Error

    I had a friend help me test my application. That testing yielded a few bugs, one of which forced me to make some changes across the board. While I finally finished this and am back to where I left off when all that came up, I'm now finding myself with a bit of a headscratcher:

    Collectively, the application houses a media catalogue. There are two parts to the catalogue: An archive of media sources (Archive), and a library that catalogues and classifies records from the Archive (Library). The short version of the reasoning here is that Archive items are unique, but the Library can contain the Archive item as well as variants thereof.

    Archive records have a "checked in" Boolean column (InLibrary) that denotes if it's been processed into the Library or not. A different form is used to add records to the Library. It reads the Archive table and populates a DataGridView (DGV) object with rows where InLibrary=False. Saving a record to the Library also updates a Counter table and changes the Archive table InLibrary value to True. But there are two exceptions:

    • The Archive table update is skipped when there are variants, in which case the table is only updated with the last variant, or
    • The item is a blank image (which will never be added), in which case the Archive table update is the only thing that happens.

    In both instances, once the writes are complete, I refresh the DGV and repopulate from the Archive table as before.

    The refresh process works great in all cases EXCEPT when the only thing taking place is the Archive table update! In this instance, the DGV refresh throws System.InvalidOperationException: 'Operation cannot be performed in this event handler.' and for the life of me, I can't figure out why. The closest thing I have found is this article: However, I'm not deleting a row; I'm clearing the DGV with .Rows.Clear() before repopulating it; in effect, doing a fresh start...

    This is the DGV handler, with the initiator highlighted in red:
        Private Sub DataGrid_SelectRow(sender As Object, e As DataGridViewCellEventArgs) Handles dgvRecords.RowEnter
            ' Select Record
            If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then                                      ' For selected table record
                strDGVCell00 = dgvRecords.Rows(e.RowIndex).Cells(0).Value                           ' Get value: Column 00
                strDGVCell01 = dgvRecords.Rows(e.RowIndex).Cells(1).Value                           ' Get value: Column 01
                If strDGVCell01 <> "" Then                                                          ' When a file is selected
                    strArchiveFileType = FLBG_ExtractString(strDGVCell01, ".", 1, 1)                ' Extract filename extension
                    For __loop = 1 To UBound(arrayFileType)                                         ' Find its ComboBox index position
                        strLibraryFileType = FLBG_ExtractString(FLBG_ExtractString(arrayFileType(__loop), "'", 1, 1), ",", 0, 1)
                        If strArchiveFileType = strLibraryFileType Then                             ' - If a match is made
                            cmbFileType.SelectedItem = strLibraryFileType                           ' - Place data in ComboBox
                            Exit For                                                                ' - Break out of the search
                        End If
                    Next                                                                            ' Repeat till done
                End If
                If Len(strDGVCell00) > 0 Then                                                       ' Assemble selected file FQN
                    strArchiveFQNDOS = FLBG_ConvertUnix2DOS(strDGVCell00 & "/") & strDGVCell01      ' Convert path to DOS format
                    strSourceFileFQN = configSourcesRoot.Substring(0, configSourcesRoot.Length - 1) & strArchiveFQNDOS
                End If
                strValue01 = FLBG_EncapsulateString(strDGVCell00, CHR_SQUOTE)                       ' Encapsulate Value
                strValue02 = FLBG_EncapsulateString(strDGVCell01, CHR_SQUOTE)                       ' Encapsulate Value
                strConditionString = strDGVColumn01 & "=" & strValue01 & " AND " &                  ' Build Lookup condition (filter)
                                     strDGVColumn02 & "=" & strValue02                              '       |
                '+ Generate Record Update Filter
                strLookupValue = FLBG_TableRecordLookup(appDataSource, tableArchive, "*", strConditionString)
                strFilterString = strLookupColumn & "=" & FLBG_EncapsulateString(strLookupValue, CHR_SQUOTE)
            End If
            '+ Get Image Data and Display in Viewport
            If IO.File.Exists(strSourceFileFQN) = True Then                                         ' Check if selected file exists
                UpdateImageViewport(True, pctImgPreview, strSourceFileFQN, btnViewImage, lblResolutionPX, lblFileSize)
                FLBG_DisplayImageFile(strSourceFileFQN, pctImgPreview)                              ' Display image
            End If
            '+ Designated Blank Frame Handler
            If strDGVCell01 <> "" Then
                If FLBG_TableColumnLookup(appDataSource, tableArchive, strFilterString, "[BlankFrame]") = True Then
                    If FLBG_CustomMsgBox(strDGVCell01 & " " & noticeBlankFrame, __ModuleLongName, 6) = vbNo Then
                        FLBG_TableRecordUpdate(appDataSource, tableArchive, "[InLibrary]=True", strFilterString)
                        Exit Sub
                    End If
                End If
            End If
        End Sub
    Here's RecycleForm(), again with the initiator highlighted in red:
        Private Sub RecycleForm()
            ' Set Form Defaults
            ResetVariables()                                                                        ' Reset variables
            UpdateImageViewport(False, pctImgPreview, strSourceFileFQN, btnViewImage, lblResolutionPX, lblFileSize)
            FLBG_PopulateDataGridFromDB(dgvRecords, appDataSource, tableArchive, strDGVColumns, "[InLibrary]=False", strDGVSortFilter)
            dgvRecords.CurrentCell = Nothing                                                        ' Disallow trigger from empty space
            grpLibrary.Select()                                                                     ' Deselect DataGridView
        End Sub
    And the ResetVariables(), where it crashes; the statement is red:
        Private Sub ResetVariables()
            ' Reset Variables
            chkAltSrc.Checked = False                                                               ' Reset CheckBox
            cmbAltSrc.SelectedIndex = -1                                                            ' Reset ComboBox
            rdoModReg.Checked = True                                                                ' Reset RadioButton
            strFileBase = ""                                                                        ' Reset: File segment
            strFileMods = ""                                                                        ' Reset: File segment
            strFileSNum = ""                                                                        ' Reset: File segment
            strFileType = ""                                                                        ' Reset: File segment
            strFileVary = ""                                                                        ' Reset: File segment
            strPathYear = ""                                                                        ' Reset: Path segment  
            strSourceFileFQN = ""                                                                   ' Reset placeholder
            lblFQN.Text = ""                                                                        ' Reset Label
            lblNumber.Text = "______"                                                               ' Reset Label
            txtNote.Clear()                                                                         ' Reset TextBox
            txtYear.Clear()                                                                         ' Reset TextBox
            txtVariant.Clear()                                                                      ' Reset TextBox
            dgvRecords.Rows.Clear()                                                                 ' Reset DataGridView
            strConditionString = ""                                                                 ' Clear placeholder
            strFilterString = ""                                                                    ' Clear placeholder
            strLookupValue = ""                                                                     ' Clear placeholder
        End Sub
    Part of me thinks I'm missing a directive before calling dgvRecords.Rows.Clear()

    The only columns in the DGV are the path and filename of the source:

    Name:  capture-a.jpg
Views: 331
Size:  38.4 KB

    And an example of when a blank frame is being processed (the error occurs if NO is selected):

    Name:  capture-b.jpg
Views: 315
Size:  43.7 KB
    Last edited by Erlkoenig; May 18th, 2024 at 05:23 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: DataGridView Error

    Quote Originally Posted by Erlkoenig View Post
    I'm not deleting a row; I'm clearing the DGV with .Rows.Clear() before repopulating it
    How exactly do you think a grid could be cleared without removing every row? You're not removing A row. You're removing EVERY row.

    For the record, I was going to try to help but your code is long, complex and very specific to your scenario, so diagnosing the issue is going to be difficult to impossible. You really should have tried to simply things for yourself already, stripping out everything that isn't specifically related to the issue. Once you remove the noise, many issues become very simple to solve. Creating simple test projects to isolate specific problem functionality is a pretty standard practice in software development and, as I said, you really should have done it for yourself and then, if you still needed help, you could show us that. As it stands, we can't test and debug your code because we can't compile just the parts you've given us. If you gave us the rest then it would be even more complex and even more of it would be irrelevant to the issue. I strongly suggest that you create a simple test project with the bare minimum of code required to reproduce the issue. You will likely have more success yourself and also more chance of finding help here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width