Results 1 to 4 of 4

Thread: [RESOLVED] DataGridView: Upper left blank cell occasionally missing when using row headers

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Resolved [RESOLVED] DataGridView: Upper left blank cell occasionally missing when using row headers

    Since row headers are not persistent in the DataGridView (DGV), I use an AddHandler to call the method below, which adds row header numbers whenever the DGV is shown or modified. You will note that the Addhandler is cancelled at the end, so that this method won't activate when a new cell/record/column is modified or the DGV is sorted by the user. It works seamlessly without any exceptions, however, on occasion the column headers are not arranged correctly (see image). Is there anything in the method's code that would be suspect for this behavior. Maybe I need to add some code?
    Code:
     Sub DGV1datachanged(ByVal sender As Object, ByVal e As EventArgs)
            If TypeOf sender Is DataGridView Then
                Dim dgv As DataGridView = CType(sender, DataGridView) 
                AddHandler dgv.Sorted, AddressOf DGV1sorted
                If dgv.Rows.Count - 1 <> NumInputObjects Then Exit Sub
                If dgv.Columns.Count = 0 Then Exit Sub
                Dim cnt As Integer = 0
                For Each row As DataGridViewRow In dgv.Rows
                    If cnt > dgv.Rows.Count - 2 Then Exit For
                    dgv.Rows(cnt).HeaderCell.Value = (cnt + 1).ToString
                    cnt += 1
                Next
                Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
                dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dgv.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
                dgv.EnableHeadersVisualStyles = True
                dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black
                dgv.RowHeadersDefaultCellStyle.ForeColor = Color.Black
                dgv.RowHeadersVisible = True
                dgv.RowHeadersWidth = (Len(cnt.ToString)) * 15
                dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                dgv.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                dgv.Refresh()
                RemoveHandler dgv.DataBindingComplete, AddressOf DGV1datachanged
            End If
    End Sub
    Name:  dgv_error.jpg
Views: 2067
Size:  46.4 KB
    Last edited by pel11; Jun 3rd, 2023 at 11:54 AM.

Tags for this Thread

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