DatagridView loses seems to lose columns

Frank Kosterman 21 Reputation points

I have an application in which a datagrid is filled with data

I create all the columns at runtime and then fill the rows

Works fine

But in the same form are several other datagridviews
When the user rightclicks on the other datagrid i perform some actions and then want to modify the content of one cell in the first datagrid

But when he tries i get a index out of bound.
In the debugger i see that row and columncount are 0

While i have several rows and 35 columns

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,321 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 19,936 Reputation points Microsoft Vendor

    Hi @Frank Kosterman ,
    The part of the code where the rows/columns are added looks fine.
    It is recommended that you do step-by-step debugging and use Watch to monitor RowCount and ColumnCount to see at which step the value goes wrong.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Frank Kosterman 21 Reputation points


    I already did that

    I created 35 columns at runtime with the command

    nColCount = rs.Fields.Count
    Me.dgView.ColumnCount = nColCount

    on the breakpoint nRow = 3 and nCell =33

    So they are all in range

    But when i inspect me.dgview.rowcount, it gives me zero

  2. Frank Kosterman 21 Reputation points
     Dim rs As ADODB.Recordset
            Dim strSql As String
            Dim nColCount As Integer
            Dim nCol As Integer
            Dim strColName As String
            Dim strColType As String
            Dim strColTemp As Object
            Dim nColWidth As Integer
            Dim nDgRow As Integer
            Dim strRegKey As String
            Dim strEnableColumn As String
            Dim nColorRow As Long
            Me.dgView.EnableHeadersVisualStyles = False
            oColType = New Collection
            strSql = "Select * from " + SetTable + "_" + strCurrentModality
            rs = New ADODB.Recordset
            rs.CursorType() = ADODB.CursorTypeEnum.adOpenDynamic
            rs.Open(strSql, cn,, ADODB.LockTypeEnum.adLockOptimistic)
            nColCount = rs.Fields.Count
            Me.dgView.ColumnCount = nColCount
            'Initialise columns
            For nCol = 0 To nColCount - 1
                strColName = rs.Fields(nCol).Name
                strColType = rs.Fields(nCol).Type
                strColTemp = rs.Fields(nCol).Properties
                nColWidth = rs.Fields(nCol).ActualSize
                Me.dgView.Columns(nCol).HeaderText = strColName
                Me.dgView.Columns(nCol).Name = strColName
                Me.dgView.Columns(nCol).SortMode = False
                Select Case strColType
                    Case cTypeString
                    Case cTypeDate
                        Me.dgView.Columns(nCol).DefaultCellStyle.Format = cDateFormat
                        Me.dgView.Columns(nCol).Width = 80
                End Select
                Me.dgView.Columns(nCol).Tag = strColType
            'Column headers done, now retrieve data
            Do While Not rs.EOF
                nDgRow = Me.dgView.Rows.Add()
                For nCol = 0 To nColCount - 1
                    dgView.Rows(nDgRow).Cells(nCol).Value = rs.Fields(nCol).Value
                nColorRow = FindColorRow(dgView.Rows(nDgRow).Cells(nColPoNumber).Value)
                If nColorRow > -1 Then
                    'Er is een kleur definitie gevonden nu verwerken
                    ProcessColors(nColorRow, nDgRow)
                End If
        End Sub
    0 comments No comments

  3. Frank Kosterman 21 Reputation points

    I went trough the code in debug mode and noticed the exact moment when the col and rowcount became zero

    It was in a function that was called from the class that i created
    that function is placed in the form

    I have 5 instances of that class in my form, btw these are also datagridviews

    Then i added some code in a combobox to call this function

    Now the col and rowcount stayed good

    So i took out the class code and placed it on the 5 instances and now it worked

       Private Sub DgInschrijvingen_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        Dim nKleur As Color
        Dim oKleur As Kleur
        If e.Button = MouseButtons.Right Then
                oKleur = frmVerzamelstaat.KiesKleur(nPos)     ' ' this will call the colordialog box
                If oKleur.IsKleurGekozen Then
                   nKleur = oKleur.GekozenKleur
                   MyBase.BackgroundColor = nKleur
                   MyBase.Columns(0).DefaultCellStyle.BackColor = nKleur
                End If
        End If
        End Sub
    0 comments No comments