加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net中dataGridView的单元格的合并

发布时间:2020-12-17 00:15:23 所属栏目:大数据 来源:网络整理
导读:vb.net中dataGridView的单元格的合并 对于下面的表格数据,我们经常会见到, 20090601 00001 100 abc 1 sumisu 20080601 00001 120 abc 1 yasio 20070601 00001 150 def 1 toms 20050601 00001 190 efg 1 arabama 但有时候我们希望将同一列中内容相同的单元
vb.net中dataGridView的单元格的合并

对于下面的表格数据,我们经常会见到,

20090601 00001 100 abc 1 sumisu
20080601 00001 120 abc 1 yasio
20070601 00001 150 def 1 toms
20050601 00001 190 efg 1 arabama

但有时候我们希望将同一列中内容相同的单元格合并,变成下面这样:
20090601 00001 100 abc 1 sumisu
20080601 120 yasio
20070601 150 def toms
20050601 190 efg arabama

那么该如何实现呢?
在网上调查了很多方法,也请教了很多人,虽然有一些解决方法,但都不是最理想的。
偶尔下载到一个例子,很漂亮地实现了这个功能,不过是C#的,于是我花了不少时间,终于将她用VB.net实现了,在这里与各位分享。

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Text
Imports System.Windows.Forms
Imports System.Collections
Imports System.Reflection
Imports System.Runtime.InteropServices

Public ClassRowMergeView ClassRowMergeView
InheritsDataGridView

ProtectedOverridesSubOnCellPainting()SubOnCellPainting(ByValeAsDataGridViewCellPaintingEventArgs)

Ife.RowIndex>-1Ande.ColumnIndex>-1Then
DrawCell(e)
EndIf

EndSub


'/<summary>
'/DrawCell
'/</summary>
'/<paramname="e"></param>
PrivateSubDrawCell()SubDrawCell(ByValeAsDataGridViewCellPaintingEventArgs)
Ife.CellStyle.Alignment=DataGridViewContentAlignment.NotSetThen
e.CellStyle.Alignment
=DataGridViewContentAlignment.MiddleCenter
EndIf
DimgridBrushAsBrush=NewSolidBrush(Me.GridColor)
'DimbackBrushAsSolidBrush=NewSolidBrush(e.CellStyle.BackColor)
DimbackBrushAsSolidBrush=NewSolidBrush(Color.White)
DimfontBrushAsSolidBrush=NewSolidBrush(e.CellStyle.ForeColor)
DimcellwidthAsInteger
DimUpRowsAsInteger=0
DimDownRowsAsInteger=0
DimcountAsInteger=0
IfMe.MergeColumnNames.Contains(Me.Columns(e.ColumnIndex).Name)Ande.RowIndex<>-1Then
cellwidth
=e.CellBounds.Width
DimgridLinePenAsPen=NewPen(gridBrush)
DimcurValueAsString=CType(e.Value,String)
IIf(curValueIsNothing,"",e.Value.ToString().Trim())
DimcurSelectedAsString=CType(Me.CurrentRow.Cells(e.ColumnIndex).Value,String)
IIf(curSelectedIsNothing,Me.CurrentRow.Cells(e.ColumnIndex).Value.ToString().Trim())
'IfNotString.IsNullOrEmpty(curValue)Then
DimiAsInteger
Fori=e.RowIndexToMe.Rows.Count-1Stepi+1
IfMe.Rows(i).Cells(e.ColumnIndex).Value.ToString().Equals(curValue)Then

DownRows
=DownRows+1
Ife.RowIndex<>iThen
cellwidth
=cellwidth
IIf(cellwidth<Me.Rows(i).Cells(e.ColumnIndex).Size.Width,cellwidth,Me.Rows(i).Cells(e.ColumnIndex).Size.Width)
EndIf
Else
ExitFor
EndIf
Next

DimjAsInteger
Forj=e.RowIndexTo0Stepj-1
IfMe.Rows(j).Cells(e.ColumnIndex).Value.ToString().Equals(curValue)Then

UpRows
=UpRows+1
Ife.RowIndex<>jThen
cellwidth
=cellwidth
IIf(cellwidth<Me.Rows(j).Cells(e.ColumnIndex).Size.Width,Me.Rows(j).Cells(e.ColumnIndex).Size.Width)
EndIf
Else
ExitFor
EndIf
Next

count
=DownRows+UpRows-1
Ifcount<2Then
Return
EndIf
'EndIf
IfMe.Rows(e.RowIndex).SelectedThen
backBrush.Color
=e.CellStyle.SelectionBackColor
fontBrush.Color
=e.CellStyle.SelectionForeColor
EndIf

e.Graphics.FillRectangle(backBrush,e.CellBounds)

PaintingFont(e,UpRows,DownRows,count)
IfDownRows=1Then
e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom
-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1)
count
=0
EndIf

e.Graphics.DrawLine(gridLinePen,e.CellBounds.Top,e.CellBounds.Bottom
-1)

e.Handled
=True
EndIf
EndSub


'/<summary>
'/PaintingFont
'/</summary>
PrivateSubPaintingFont()SubPaintingFont(ByValeAsSystem.Windows.Forms.DataGridViewCellPaintingEventArgs,ByValcellwidthAsInteger,ByValUpRowsAsInteger,ByValDownRowsAsInteger,ByValcountAsInteger)
DimfontBrushAsSolidBrush=NewSolidBrush(e.CellStyle.ForeColor)
DimfontheightAsInteger=CType(e.Graphics.MeasureString(e.Value.ToString(),e.CellStyle.Font).Height,Integer)
DimfontwidthAsInteger=CType(e.Graphics.MeasureString(e.Value.ToString(),e.CellStyle.Font).Width,Integer)
DimcellheightAsInteger=e.CellBounds.Height

Ife.CellStyle.Alignment=DataGridViewContentAlignment.BottomCenterThen
e.Graphics.DrawString(
CType(e.Value,String),e.CellStyle.Font,fontBrush,CType(e.CellBounds.X+(cellwidth-fontwidth)/2,Single),e.CellBounds.Y+cellheight*DownRows-fontheight)
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.BottomLeftThen
e.Graphics.DrawString(
CType(e.Value,e.CellBounds.X,e.CellBounds.Y+cellheight*DownRows-fontheight)
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.BottomRightThen
e.Graphics.DrawString(
CType(e.Value,e.CellBounds.X+cellwidth-fontwidth,e.CellBounds.Y+cellheight*DownRows-fontheight)
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.MiddleCenterThen
e.Graphics.DrawString(
CType(e.Value,CType(e.CellBounds.Y-cellheight*(UpRows-1)+(cellheight*count-fontheight)/2,Single))
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.MiddleLeftThen
e.Graphics.DrawString(
CType(e.Value,Single))
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.MiddleRightThen
e.Graphics.DrawString(
CType(e.Value,Single))
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.TopCenterThen
e.Graphics.DrawString(
CType(e.Value,e.CellBounds.X+CType((cellwidth-fontwidth)/2,e.CellBounds.Y-cellheight*(UpRows-1))
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.TopLeftThen
e.Graphics.DrawString(
CType(e.Value,e.CellBounds.Y-cellheight*(UpRows-1))
ElseIfe.CellStyle.Alignment=DataGridViewContentAlignment.TopRightThen
e.Graphics.DrawString(
CType(e.Value,e.CellBounds.Y-cellheight*(UpRows-1))
Else
e.Graphics.DrawString(
CType(e.Value,Single))
EndIf
EndSub


'/<summary>
'/MergeColumnNames
'/</summary>
PublicPropertyMergeColumnNames()PropertyMergeColumnNames()AsList(OfString)
Get
Return_mergecolumnname
EndGet
Set(ByValValueAsList(OfString))
_mergecolumnname
=Value
EndSet
EndProperty

Private_mergecolumnnameAsList(OfString)=NewList(OfString)()

EndClass


Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms


Public ClassForm1 ClassForm1

PublicSubNew()SubNew()
InitializeComponent()
DimdtAsDataTable=NewDataTable()
DimiAsInteger
dt.Columns.Add(
"1")
dt.Columns.Add(
"2")
dt.Columns.Add(
"3")
dt.Columns.Add(
"4")
dt.Columns.Add(
"5")
dt.Columns.Add(
"6")
dt.Rows.Add(
"20090601","00001","100","abc","1","sumisu")
dt.Rows.Add(
"20080601","120","yasio")
dt.Rows.Add(
"20070601","150","def","toms")
dt.Rows.Add(
"20050601","190","efg","arabama")
Me.rowMergeView1.DataSource=dt
Me.rowMergeView1.ColumnHeadersHeight=20
Me.rowMergeView1.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.DisableResizing
Fori=1Todt.Columns.Count
Me.rowMergeView1.MergeColumnNames.Add("Column"&i)
Next
EndSub

EndClass


< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() > _
Partial
ClassForm1 ClassForm1
InheritsSystem.Windows.Forms.Form

'フォームがコンポーネントの一覧をクリーンアップするためにdisposeをオーバーライドします。
<System.Diagnostics.DebuggerNonUserCode()>_
ProtectedOverridesSubDispose()SubDispose(ByValdisposingAsBoolean)
Try
IfdisposingAndAlsocomponentsIsNotNothingThen
components.Dispose()
EndIf
Finally
MyBase.Dispose(disposing)
EndTry
EndSub


'Windowsフォームデザイナで必要です。
PrivatecomponentsAsSystem.ComponentModel.IContainer

'メモ:以下のプロシージャはWindowsフォームデザイナで必要です。
'Windowsフォームデザイナを使用して変更できます。
'コードエディタを使って変更しないでください。
<System.Diagnostics.DebuggerStepThrough()>_
PrivateSubInitializeComponent()SubInitializeComponent()
DimresourcesAsSystem.ComponentModel.ComponentResourceManager=NewSystem.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.Panel1=NewSystem.Windows.Forms.Panel
Me.Button1=NewSystem.Windows.Forms.Button
Me.rowMergeView1=NewtestMearge.RowMergeView
Me.Column1=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Column2=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Column3=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Column4=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Column5=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Column6=NewSystem.Windows.Forms.DataGridViewTextBoxColumn
Me.Panel1.SuspendLayout()
CType(Me.rowMergeView1,System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Controls.Add(Me.Button1)
Me.Panel1.Dock=System.Windows.Forms.DockStyle.Bottom
Me.Panel1.Location=NewSystem.Drawing.Point(0,312)
Me.Panel1.Name="Panel1"
Me.Panel1.Size=NewSystem.Drawing.Size(595,31)
Me.Panel1.TabIndex=1
'
'Button1
'
Me.Button1.Location=NewSystem.Drawing.Point(499,5)
Me.Button1.Name="Button1"
Me.Button1.Size=NewSystem.Drawing.Size(75,23)
Me.Button1.TabIndex=0
Me.Button1.Text="Button1"
Me.Button1.UseVisualStyleBackColor=True
'
'rowMergeView1
'
Me.rowMergeView1.AllowUserToAddRows=False
Me.rowMergeView1.AutoSizeColumnsMode=System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
Me.rowMergeView1.ColumnHeadersHeightSizeMode=System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.rowMergeView1.Columns.AddRange(NewSystem.Windows.Forms.DataGridViewColumn(){Me.Column1,Me.Column2,Me.Column3,Me.Column4,Me.Column5,Me.Column6})
Me.rowMergeView1.Dock=System.Windows.Forms.DockStyle.Fill
Me.rowMergeView1.Location=NewSystem.Drawing.Point(0,0)
Me.rowMergeView1.MergeColumnNames=CType(resources.GetObject("rowMergeView1.MergeColumnNames"),System.Collections.Generic.List(OfString))
Me.rowMergeView1.Name="rowMergeView1"
Me.rowMergeView1.RowTemplate.Height=21
Me.rowMergeView1.Size=NewSystem.Drawing.Size(595,343)
Me.rowMergeView1.TabIndex=0
'
'Column1
'
Me.Column1.DataPropertyName="1"
Me.Column1.HeaderText="日期"
Me.Column1.Name="Column1"
'
'Column2
'
Me.Column2.DataPropertyName="2"
Me.Column2.HeaderText="代码"
Me.Column2.Name="Column2"
'
'Column3
'
Me.Column3.DataPropertyName="3"
Me.Column3.HeaderText="价格"
Me.Column3.Name="Column3"
'
'Column4
'
Me.Column4.DataPropertyName="4"
Me.Column4.HeaderText="备注"
Me.Column4.Name="Column4"
'
'Column5
'
Me.Column5.DataPropertyName="5"
Me.Column5.HeaderText="标志"
Me.Column5.Name="Column5"
'
'Column6
'
Me.Column6.DataPropertyName="6"
Me.Column6.HeaderText="更新者"
Me.Column6.Name="Column6"
'
'Form1
'
Me.AutoScaleDimensions=NewSystem.Drawing.SizeF(6.0!,12.0!)
Me.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize=NewSystem.Drawing.Size(595,343)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.rowMergeView1)
Me.Name="Form1"
Me.Text="Form1"
Me.Panel1.ResumeLayout(False)
CType(Me.rowMergeView1,System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

EndSub

FriendWithEventsrowMergeView1AsRowMergeView
FriendWithEventsPanel1AsSystem.Windows.Forms.Panel
FriendWithEventsButton1AsSystem.Windows.Forms.Button
FriendWithEventsColumn1AsSystem.Windows.Forms.DataGridViewTextBoxColumn
FriendWithEventsColumn2AsSystem.Windows.Forms.DataGridViewTextBoxColumn
FriendWithEventsColumn3AsSystem.Windows.Forms.DataGridViewTextBoxColumn
FriendWithEventsColumn4AsSystem.Windows.Forms.DataGridViewTextBoxColumn
FriendWithEventsColumn5AsSystem.Windows.Forms.DataGridViewTextBoxColumn
FriendWithEventsColumn6AsSystem.Windows.Forms.DataGridViewTextBoxColumn

EndClass


运行结果如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读