DataGridView 单元格合并问题——无法释放动作

knums 2017-08-25 06:31:58
前情:
最近,从网上下载一段代码(类),功能是DataGridView 的单元格合并,调用成功,可以对任意相邻的一些单元格进行合并;

问题:
在一个窗体中,只有一个DataGridView ,数据是动态加载的,也就是能通过按扭切换,加载不同的数据 。

当表格显示一组数据,第一次调用这个合并单元格类后,在这一个表格中能按要求合并单元格,但是,当按下按扭,加载新的数据时(同一个DataGridView ,显示完全不同的数据),虽然没有在切换时加入合并单元格动作,但这些合并动作还在,也就是在新的显示表格中,依然还在进行合并。
查看源码,并没有释放资源取消动作的功能,也不知道怎么实现这功能,请高手指教;

合并类及调用代码如下(网上下载的):

‘=====================================
'·合并单元格类
'***********************************************************************

Public Class CmbDatagridbiew
Private data As New List(Of MyRect)
Private Dgv As DataGridView
Public Sub New(_dgv As DataGridView)
Me.Dgv = _dgv
AddHandler _dgv.CellPainting, AddressOf DGV_CellPainting
End Sub
Public Sub Close()
data.Clear()
End Sub
Public Sub Add(_rect As MyRect)
Me.data.Add(_rect)
Me.SetCellEnabled(_rect)
End Sub

Public Sub Add(_top As Integer, _left As Integer, _bottom As Integer, _right As Integer)
Me.data.Add(New MyRect(_top, _left, _bottom, _right))
Me.SetCellEnabled(New MyRect(_top, _left, _bottom, _right))
End Sub

Private Sub SetCellEnabled(_rect As MyRect)
For i = _rect.Top To _rect.Bottom
For j = _rect.Left To _rect.Right
Me.Dgv.Rows(i).Cells(j).ReadOnly = True
Next
Next
End Sub

Private Function InRects(rowIndex As Integer, colIndex As Integer) As Integer
For i = 0 To Me.data.Count - 1
If Me.data(i).InRect(rowIndex, colIndex) Then Return i
Next
Return -1
End Function

Private Sub DGV_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs)
Using gridBrush As Brush = New SolidBrush(Me.Dgv.GridColor),
backColorBrush As SolidBrush = New SolidBrush(e.CellStyle.BackColor)
Using gridLinePen = New Pen(gridBrush)
If Me.data.Count = 0 Then Return
Dim index As Integer = Me.InRects(e.RowIndex, e.ColumnIndex)
If index = -1 Then Return
e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
If e.RowIndex = Me.data(index).Bottom Then e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Left, e.CellBounds.Bottom - 1,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
If e.ColumnIndex = Me.data(index).Right Then e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Right - 1, e.CellBounds.Top,
e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
e.Handled = True
For i = 0 To Me.data.Count - 1
Dim rect1 As Rectangle = Me.Dgv.GetCellDisplayRectangle(Me.data(i).Left, Me.data(i).Top, False)
Dim rect2 As Rectangle = Me.Dgv.GetCellDisplayRectangle(Me.data(i).Right, Me.data(i).Bottom, False)
Dim rect As New Rectangle(rect1.Left, rect1.Top, rect2.Right - rect1.Left, rect2.Bottom - rect1.Top)
Dim text As String
Try
text =
Me.Dgv.Rows(Me.data(i).Top).Cells(Me.data(i).Left).Value.ToString().Trim()
Catch ex As Exception
text = ""
End Try
Dim sz As Drawing.SizeF = e.Graphics.MeasureString(text, e.CellStyle.Font)
e.Graphics.DrawString(text, e.CellStyle.Font, New SolidBrush(e.CellStyle.ForeColor),
rect.Left + (rect.Width - sz.Width) / 2,
rect.Top + (rect.Height - sz.Height) / 2,
StringFormat.GenericDefault)
Next
End Using
End Using
End Sub

End Class

Public Class MyRect
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
Public Left As Integer
Public Sub New(_top As Integer, _left As Integer, _bottom As Integer, _right As Integer)
Me.Top = _top
Me.Right = _right
Me.Bottom = _bottom
Me.Left = _left
End Sub

Public Function InRect(rowIndex As Integer, colIndex As Integer) As Boolean
If rowIndex >= Me.Top And rowIndex <= Me.Bottom And colIndex >= Me.Left And colIndex <= Me.Right Then
Return True
Else
Return False
End If
End Function
End Class

'#End CmbDatagridbiew

’==========================================以下 调用代码

'·调用示例:
' 声明
' Private cmb As MergeCells
'
' 表格加载完成后
' cmb = New CmbDatagridbiew(Me.DataGridView1)
' cmb.Add(0, 0, 1, 2)
‘合并范围
’ 起点单元格中(0,0)即第一行第一列交叉位置的单元格,终点单元格(1,2)即第二行第三列交叉位置的单元格

’**************************************************************************************
**************************************************************************************
‘================================================以下实际操作演示

Public Class Form1

Private cmb As MergeCells

Private Sub Fm_data_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

‘ 窗体中只有一个固定的DataGridView,二个按扭(以显示不同的数据)

end sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘按扭一 加载数据到表格中
.......

’加入合并动作
cmb = New CmbDatagridbiew(Me.DataGridView1)
' cmb.Add(0, 0, 1, 2)

End Sub ’能正常加载并按要求合并单元格


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘按扭二,再加载全新的数据到表格中(同一个DataGridView)
.......

’没有加入合并动作

End Sub ’问题出现——上次的合并动作仍然在起作用,将同样的单元格进行了合并,但这不是我需要的


end class


请高手指教,如何解决这个问题?




...全文
274 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
knums 2017-08-25
  • 打赏
  • 举报
回复
类代码没有问题,调用成功,只是有一个不能释放资源(动作)的BUG!
平头哥哥 2017-08-25
  • 打赏
  • 举报
回复
debug过?通篇代码,
平头哥哥 2017-08-25
  • 打赏
  • 举报
回复
debug过?通篇代码,

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧