Results 1 to 4 of 4

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

  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: 343
Size:  46.4 KB
    Last edited by pel11; Jun 3rd, 2023 at 11:54 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: DataGridView: Upper left blank cell occasionally missing when using row headers

    It appears that after I moved the following three lines before the loop that adds row numbers to the row header values, it will work. I checked if dgv.TopLeftHeaderCell.Visible was True, and it was. So its width was just not getting set to the width of the maximum number of rows. Now, I set the row header width before adding row numbers, and it seems to be resolved.

    Code:
      dgv.RowHeadersWidth = (Len(dgv.Rows.Count.ToString)) * 20
      dgv.RowHeadersDefaultCellStyle.ForeColor = Color.Black
      dgv.RowHeadersVisible = True

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: DataGridView: Upper left blank cell occasionally missing when using row headers

    If your problem has been resolved, please use the Thread Tools menu to mark the thread Resolved, so everyone knows that you don't need any further help without opening the thread and reading the whole thing. I have done so for you this time.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2022
    Posts
    83

    Re: DataGridView: Upper left blank cell occasionally missing when using row headers

    Thanks!

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