如何合并datagridview的两个列标题 [英] How to merge two column headers of a datagridview
本文介绍了如何合并datagridview的两个列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要合并两个列标题,并为其提供Windows窗体的新标题
像这样:
I need to merge two column headers and give it a new header for Windows Forms
Like this:
----------------------------------------
SNO | Name | English | Maths | Total |
| | Th | Prac | | |
----------------------------------------
| | | | | |
| | | | | |
| | | | | |
----------------------------------------
如果有人可以帮助,请稍候.
Kinda urgent, if somebody may help.
推荐答案
e.g
假设在网格中有此列,
星期一1
星期一2
tues1
tues2
wed1
wed2
e.g
suppose in grid this columns,
Mon1
Mon2
tues1
tues2
wed1
wed2
Private Sub dataGridView1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles DataGridView1.Paint
Try
Dim colList ="Mon,tues,wed"
Dim clst As String() = colList.ToString.Split(",")
Dim j As Integer = 0
While j < DataGridView1.ColumnCount - 1 '10
Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True) 'see -1 that is row=-1 means header cell
Dim w2 As Integer = Me.DataGridView1.GetCellDisplayRectangle(j + 1, -1, True).Width
r1.X += 0
r1.Y += 0
' r1.Width = r1.Width + w2 -2
r1.Width = ((DataGridView1.ColumnCount / clst.Count) * w2)
r1.Height = r1.Height / 2
e.Graphics.FillRectangle(New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1)
Dim format As New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(clst(j / ((DataGridView1.ColumnCount / (clst.Count + 1)) + 1)), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
j += (DataGridView1.ColumnCount / clst.Count)
End While
Catch ex As Exception
End Try
End Sub
祝您编码愉快!
:)
Happy Coding!
:)
这篇关于如何合并datagridview的两个列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文