DataGridView单元格合并VB实现
更新时间:2024-01-25 21:43:01 阅读量: 教育文库 文档下载
DataGridView单元格合并VB.NET实现
已封装成类,调用方便。 效果如下:
Public Class Form1
Private cmb As CmbDatagridbiew
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load '测试数据
DataGridView1.ColumnCount = 6 DataGridView1.Columns(0).Name = \ DataGridView1.Columns(1).Name = \ DataGridView1.Columns(2).Name = \ DataGridView1.Columns(3).Name = \ DataGridView1.Columns(4).Name = \ DataGridView1.Columns(5).Name = \
Dim row As String() = New String() {\, \, \, \, \, \} : DataGridView1.Rows.Add(row)
row = New String() {\, \, \, \, \, \} : DataGridView1.Rows.Add(row)
row = New String() {\, \, \, \, \, \} : DataGridView1.Rows.Add(row)
row = New String() {\, \, \, \, \, \} : DataGridView1.Rows.Add(row)
'调用
cmb = New CmbDatagridbiew(Me.DataGridView1)
cmb.Add(0, 0, 1, 2) '要合并的单元格·([行索引·左上],[列索引·左上],[行索引·右下],[列索引·右下])
cmb.Add(1, 3, 3, 4) '要合并的单元格·([行索引·左上],[列索引·左上],[行索引·右下],[列索
引·右下])
End Sub End Class
'*********************************************************************** '*********************************************************************** '·合并单元格类 '·调用示例: ' 声明
' Private cmb As CmbDatagridbiew '
' 表格加载完成后
' cmb = New CmbDatagridbiew(Me.DataGridView1) ' cmb.Add(0, 0, 1, 2)
' * 参数说明:([行索引·左上],[列索引·左上],[行索引·右下],[列索引·右下]) ' * 【0,0】 【0,1】 【0,2】 ' * 【1,0】 【1,1】 【1,2】 ' * cmb.Add(0, 0, 1, 2)
'*********************************************************************** 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 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
'***********************************************************************
作者:西门吹雪
玉树临风工作室荣誉出品
正在阅读:
奇妙的月光作文450字06-28
大连金广枫景施工组织设计03-27
历史教学中学生人格教育的“另类”探析03-18
秋之赞歌作文500字06-25
第2章 传质分离过程热力学基础06-03
脱硝设备检修工艺规程 - 图文01-02
桃花我想对你说作文450字07-09
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- DataGridView
- 合并
- 单元
- 实现
- 上海七年级第二学期数学期末数学考试试卷(12)
- 走近孤独孩子的内心
- 浅论秘书人员的素养
- 2016-5-10-案例-怀孕女工未请假连续缺勤,用人单位有权解除劳动合同吗?
- 热力学重点第3章 - secret
- 六年级语文上册《梅花魂》教学设计
- 理论复习题
- 计算机网络安全毕业论文
- 长沙市市政公用工程方案及初步设计审查要点(修订)
- 2×20MW屋顶分布式光伏发电项目申请报告 - 图文
- 教师的八个职业道德习惯
- 人教版五年级下册第二次月考测试卷
- 无人机驾驶员试题库
- 轨道交通车辆段上盖平台与房地产综合开发分析
- 影响美拉德反应的因素
- 敬老院建设项目可行性研究报告 - 图文
- 天海污北仓污水处理厂工程施工组织设计方案
- 地方税务局稽查局工作职责规范
- 亿栋商务旅游酒店可行性研究报告
- 三年级劳动与技术上册教案