-
Jun 3rd, 2023, 11:49 AM
#1
Thread Starter
Lively Member
[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
Last edited by pel11; Jun 3rd, 2023 at 11:54 AM.
-
Jun 3rd, 2023, 03:44 PM
#2
Thread Starter
Lively Member
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
-
Jun 3rd, 2023, 11:09 PM
#3
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.
-
Jun 4th, 2023, 11:56 AM
#4
Thread Starter
Lively Member
Re: DataGridView: Upper left blank cell occasionally missing when using row headers
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|