



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 = ID DataGridView1.Columns(1).Name = Name DataGridView1.Columns(2).Name = Price DataGridView1.Columns(3).Name = Month DataGridView1.Columns(4).Name = Lever DataGridView1.Columns(5).Name = ad Dim row As String() = New String() 1, Product 1, 1000, 1, high, no : DataGridView1.Rows.Add(row) row = New String() 2, Product 2, 2000, 3, high, no : DataGridView1.Rows.Add(row) row = New String() 3, Product 3, 3000, 6, mid, no : DataGridView1.Rows.Add(row) row = New String() 4, Product 4, 4000, 9, low, no : DataGridView1.Rows.Add(row) 调用 cmb = New CmbDatagridbiew(Me.DataGridView1) cmb.Add(0, 0, 1, 2) 要合并的单元格(行索引左上,列索引左上,行索引右下,列索引右下) cmb.Add(1, 3, 3, 4) 要合并的单元格(行索引左上,列索引左上,行索引右下,列索引右下) End SubEnd 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 SubEnd ClassPublic 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 纺织服饰品牌文化传播规划
- 2025浙江温州龙港市市属国有企业面向社会招聘工作人员21人笔试备考题库及答案解析
- 企业管理中的领导力发挥
- 印刷品质量管理细则
- 2025云南丽江市古城区司法局招聘司法行政辅助人员1人考试备考试题及答案解析
- 2025信息安全工程师招聘题库及答案
- 员工职业道德修养课程
- 2025年碳资产管理师考试题库(附答案和详细解析)
- 2025年天津上半年房地产估价师制度与政策商品房销售中禁止的行为试题
- 2025年流行病学流行病调查与预防控制计划模拟卷答案及解析
- 托管班合伙人合同协议书
- 9《犟龟》公开课一等奖创新教学设计
- 2025劳动合同补充协议
- 社区节水节电知识培训课件
- 防火墙行业知识培训课件
- 2025版全新升级二手房买卖合同模板下载
- 乡镇执法证考试题及答案
- 2025年监理工程师继续教育试卷及答案
- 2025滨海投资(天津)有限公司校园招聘考试备考题库及答案解析
- 2024-2025学年度江西建设职业技术学院单招《职业适应性测试》题库试题【名师系列】附答案详解
- 2020-2025年注册土木工程师(水利水电)之专业基础知识通关考试题库带答案解析
评论
0/150
提交评论