單元格合并VB實(shí)現(xiàn)_第1頁(yè)
單元格合并VB實(shí)現(xiàn)_第2頁(yè)
單元格合并VB實(shí)現(xiàn)_第3頁(yè)
單元格合并VB實(shí)現(xiàn)_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、DataGridView單元格合并VB.NET實(shí)現(xiàn)已封裝成類,調(diào)用方便。效果如下:Public Class Form1 Private cmb As CmbDatagridbiew Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load '測(cè)試數(shù)據(jù) DataGridView1.ColumnCount = 6 DataGridView1.Columns(0).Name = "ID" DataGridView1.Columns(1).Name = "Name&qu

2、ot; 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&

3、quot;, "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", &q

4、uot;6", "mid", "no" : DataGridView1.Rows.Add(row) row = New String() "4", "Product 4", "4000", "9", "low", "no" : DataGridView1.Rows.Add(row) '調(diào)用 cmb = New CmbDatagridbiew(Me.DataGridView1) cmb.Add(0, 0, 1, 2) &

5、#39;要合并的單元格·(行索引·左上,列索引·左上,行索引·右下,列索引·右下) cmb.Add(1, 3, 3, 4) '要合并的單元格·(行索引·左上,列索引·左上,行索引·右下,列索引·右下) End SubEnd Class'*'*'·合并單元格類'·調(diào)用示例:' 聲明' Private cmb As CmbDatagridbiew'' 表格加載完成后' cmb = New CmbDat

6、agridbiew(Me.DataGridView1)' cmb.Add(0, 0, 1, 2)' * 參數(shù)說明:(行索引·左上,列索引·左上,行索引·右下,列索引·右下)' * 【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

7、 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 MyRec

8、t(_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(rowInd

9、ex 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.Gri

10、dColor), 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.Row

11、Index = 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

12、- 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(

13、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.F

14、ont) 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, colIndex As Integer) As

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論