Datagridview column header merge problem on mouse hover vb.net

  • Thread starter Thread starter GEEDER
  • Start date Start date
G

GEEDER

Guest
hi! just want to know if is there any fix in this problem?


image:


1359433.png


as you can see im merging columns in my datagridview. reference : Answer by sir Mark Liu-lxf


the problem is when I hover the mouse on the column header the paint disappears.


Private Sub dgvMain_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvMain.CellPainting
If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
Dim r2 As Rectangle = e.CellBounds
r2.Y += e.CellBounds.Height / 3
r2.Height = e.CellBounds.Height / 3
e.PaintBackground(r2, True)
e.PaintContent(r2)
e.Handled = True
End If
End Sub


Private Sub dgvMain_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgvMain.ColumnWidthChanged
Dim rtHeader As Rectangle = Me.dgvMain.DisplayRectangle
rtHeader.Height = Me.dgvMain.ColumnHeadersHeight / 3
Me.dgvMain.Invalidate(rtHeader)
End Sub


Private Sub dgvMain_Paint(sender As Object, e As PaintEventArgs) Handles dgvMain.Paint
_priclsMaintenance = New pdmclsStandardFinishTabulation
_priclsMaintenance.ConnectionString = MyMSSQLServer2000ConnectionString
If MyMSSQLServer2000ConnectionString = Nothing Then
MessageBox.Show("Cannot connect to the server.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If

Dim _StaticColumns As String() = {"Materials and Finish"}
Dim xx As Integer = 0
While xx < 3
Dim r1 As RectangleF = dgvMain.GetCellDisplayRectangle(xx, -1, True)
Dim w2 As Integer = dgvMain.GetCellDisplayRectangle(xx + 1, -1, True).Width
Dim w3 As Integer = dgvMain.GetCellDisplayRectangle(2, -1, True).Width
r1.X += 1
r1.Y += 1
r1.Width = r1.Width + w2 + w3 - 2
r1.Height = r1.Height / 1 - 2
e.Graphics.FillRectangle(New SolidBrush(dgvMain.ColumnHeadersDefaultCellStyle.BackColor), r1)
Dim format As New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(_StaticColumns(xx \ 3), dgvMain.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(dgvMain.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
xx += 3
End While

Dim _dimdtsLoadColumns As DataSet = ExecuteDataSet(Mode.InitialColumns)
Dim _MainColumns() As String = New String((_dimdtsLoadColumns.Tables(0).Rows.Count) - 1) {}
Dim loopcounter As Integer = 0
Do While (loopcounter < _dimdtsLoadColumns.Tables(0).Rows.Count)
_MainColumns(loopcounter) = _dimdtsLoadColumns.Tables(0).Rows(loopcounter)("MainCols").ToString
loopcounter = (loopcounter + 1)
Loop
Dim x As Integer = 3
Dim i As Integer = 0
Dim ColCounter As Integer = dgvMain.ColumnCount - 3

While i < ColCounter
Dim r1 As Rectangle = dgvMain.GetCellDisplayRectangle(x, -1, True)
Dim w2 As Integer = dgvMain.GetCellDisplayRectangle(x + 1, -1, True).Width
r1.X += 1
r1.Y += 1
r1.Width = r1.Width + w2 + w2 - 3
r1.Height = r1.Height / 2 - 2
e.Graphics.FillRectangle(New SolidBrush(dgvMain.ColumnHeadersDefaultCellStyle.BackColor), r1)
Dim format As New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(_MainColumns(i / 3), dgvMain.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(dgvMain.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
x += 3
i += 3
End While
End Sub


Private Sub dgvMain_Scroll(sender As Object, e As ScrollEventArgs) Handles dgvMain.Scroll
Dim rtHeader As Rectangle = Me.dgvMain.DisplayRectangle
rtHeader.Height = Me.dgvMain.ColumnHeadersHeight / 3
Me.dgvMain.Invalidate(rtHeader)
End Sub

hope you can help me thanks.

Continue reading...
 
Back
Top