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![]()




Reply With Quote
