• 为CGridHelper增加合并单元格的能力


    一、关于起因

    昨天刚刚完成了CGridHelper的部分代码,今天的需求就变动了,需要为这个类添加合并单元格内容的能力。DataGridView控件本身没有提供这个功能,需要自己写代码实现。首先想到的是拿来主义,网上搜罗了一下,找了段C#代码,翻译成Vb.net之后呈现给大家。最终实现的效果如下图所示:

    二、CGridHelper新的调用代码
    1         Dim mGridHelper As New CGridHelper(GridVersion, Color.FromArgb(250, 250, 250),
    2             New Font("微软雅黑", 10), 30, Color.FromArgb(159, 210, 235), Color.FromArgb(0, 0, 0), 24,
    3             New Font("微软雅黑", 9), 30, Color.FromArgb(250, 250, 250), Color.FromArgb(225, 225, 225), Color.FromArgb(0, 0, 0),
    4             "0;80;70;595", "M;M;M;L", "1;2")

    新的调用通过最后一个MergeColumn字符串参数设置,将需要合并的列号以逗号进行分隔。

    三、CGridHelper变动部分代码
     1     Private WithEvents mGrid As DataGridView
     2 
     3     Private mMergeColumn As String
     4     Public Property MergeColumn() As String
     5         Get
     6             Return mMergeColumn
     7         End Get
     8         Set(ByVal value As String)
     9             mMergeColumn = value
    10         End Set
    11     End Property
    12 
    13     Private Function GetMergeComumns() As List(Of Integer)
    14         If Me.MergeColumn Is Nothing OrElse Me.MergeColumn.Length = 0 Then Return Nothing
    15         Dim mCol As List(Of String) = Me.MergeColumn.Split(";").ToList
    16         Dim rtl As New List(Of Integer)
    17         For Each str As String In mCol
    18             On Error Resume Next
    19             rtl.Add(CType(str, Integer))
    20         Next
    21         Return rtl
    22     End Function
    23 
    24     Private Sub mGrid_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles mGrid.CellPainting
    25         Dim mCols As List(Of Integer) = Me.GetMergeComumns()
    26         If mCols Is Nothing OrElse mCols.Count = 0 Then Exit Sub
    27         If e.RowIndex = -1 Then Exit Sub
    28         If Not mCols.Exists(Function(v As Integer) v = e.ColumnIndex) Then Exit Sub
    29         Using gridBrush As New SolidBrush(Me.Grid.GridColor), backColorBrush As New SolidBrush(e.CellStyle.BackColor)
    30             Using gridLinePen As New Pen(gridBrush)
    31                 e.Graphics.FillRectangle(backColorBrush, e.CellBounds)  '//清除单元格
    32                 '// 画 Grid 边线(仅画单元格的底边线和右边线)
    33                 '// 如果下一行和当前行的数据不同,则在当前的单元格画一条底边线
    34                 If e.RowIndex < Me.Grid.Rows.Count - 1 AndAlso
    35                     Me.Grid.Rows(e.RowIndex + 1).Cells(e.ColumnIndex).Value.ToString <> e.Value.ToString Then
    36                     e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
    37                 End If
    38                 '//画最后一条记录的底线 
    39                 If e.RowIndex = Me.Grid.Rows.Count - 1 Then
    40                     e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left + 2, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
    41                 End If
    42                 '//画右边线
    43                 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
    44                 '// 画(填写)单元格内容,相同的内容的单元格只填写第一个
    45                 If Not e.Value Is Nothing Then
    46                     If Not (e.RowIndex > 0 AndAlso Me.Grid.Rows(e.RowIndex - 1).Cells(e.ColumnIndex).Value.ToString = e.Value.ToString) Then
    47                         e.Graphics.DrawString(e.Value, e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault)
    48                     End If
    49                     e.Handled = True
    50                 End If
    51             End Using
    52         End Using
    53     End Sub
  • 相关阅读:
    jQuery表单选择器 安静点
    设计模式建造者模式
    设计模式组合模式
    设计模式单例模式
    简述ASP.NET网站开发步骤
    设计模式适配器模式
    设计模式工厂方法模式
    设计模式桥接模式
    设计模式装饰模式
    设计模式抽象工厂方法模式
  • 原文地址:https://www.cnblogs.com/alexywt/p/6708143.html
Copyright © 2020-2023  润新知