VB.NET Gridview合并单元格实例
沉默老哥 2010-11-29 09:22:12 前段时间在编写Gridview合并单元格问题时遇到了一点问题。我用的是VB.NET,而在网上搜索的都是C++语言。今将我个人摸索的用VB.NET编写的Gridview合并单元格的程序推荐给大家。希望能对大家有所帮助。
VB.NET Gridview合并单元格实例
Dim I As Integer = 1
Dim IISA As Integer = 0
Dim str1 As String
Dim str2 As String
‘合并第一列中信息相同的行
str1 = GridView1.Rows(0).Cells(0).Text ‘第一列第一行的的值
For I = 0 To GridView1.Rows.Count - 1
str2 = GridView1.Rows(I).Cells(0).Text ‘第i列第一行的的值
If str2 = str1 Then
IISA = I + 1 ‘与第一行的值相同的行数
ElseIf str2 <> str1 Then
Exit For
End If
Next
If IISA = GridView1.Rows.Count Then ‘第一列所有行的值相同
IISA = 0 ‘当第一列所有行的值相同时取IISA=0
Dim oldtc As TableCell = GridView1.Rows(IISA).Cells(0) ‘定义第一列第IISA=0行的单元格
For I = 0 To GridView1.Rows.Count - 1
Dim tc As TableCell = GridView1.Rows(I).Cells(0) ‘定义第一列第 I 行的单元格
If oldtc.Text = tc.Text Then
If oldtc.RowSpan = 0 Then
oldtc.RowSpan = 1
Else
oldtc.RowSpan = oldtc.RowSpan + 1
oldtc.VerticalAlign = VerticalAlign.Middle
tc.Visible = False
End If
Else
oldtc = tc
End If
Next
Else ‘第一列中的行有不同的值,IISA<>0
Dim oldtc As TableCell = GridView1.Rows(IISA).Cells(0) ‘定义第一列第IISA行的单元格
For I = 0 To GridView1.Rows.Count - 1
Dim tc As TableCell = GridView1.Rows(I).Cells(0) ‘定义第一列第 I 行的单元格
If oldtc.Text = tc.Text Then
tc.Visible = False
If oldtc.RowSpan = 0 Then
oldtc.RowSpan = 1
End If
oldtc.RowSpan = oldtc.RowSpan + 1
oldtc.VerticalAlign = VerticalAlign.Middle
Else
oldtc = tc
End If
Next
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
‘合并第二列中信息相同的行,其他列可以仿此来做。
str1 = GridView1.Rows(0).Cells(1).Text ‘第二列第一行的的值
For I = 0 To GridView1.Rows.Count - 1
str2 = GridView1.Rows(I).Cells(1).Text ‘第i列第一行的的值
If str2 = str1 Then
IISA = I + 1 ‘与第一行的值相同的行数
ElseIf str2 <> str1 Then
Exit For
End If
Next
If IISA = GridView1.Rows.Count Then ‘第二列所有行的值相同
IISA = 0 ‘当第二列所有行的值相同时取IISA=0
Dim oldtc1 As TableCell = GridView1.Rows(IISA).Cells(1) ‘定义第二列第IISA=0行的单元格
For I = 0 To GridView1.Rows.Count - 1
Dim tc As TableCell = GridView1.Rows(I).Cells(1) ‘定义第二列第 I 行的单元格
If oldtc1.Text = tc.Text Then
If oldtc1.RowSpan = 0 Then
oldtc1.RowSpan = 1
Else
oldtc1.RowSpan = oldtc1.RowSpan + 1
oldtc1.VerticalAlign = VerticalAlign.Middle
tc.Visible = False
End If
Else
oldtc1 = tc
End If
Next
Else ‘第二列中的行有不同的值,IISA<>0
Dim oldtc1 As TableCell = GridView1.Rows(IISA).Cells(1) ‘定义第二列第IISA行的单元格
For I = 0 To GridView1.Rows.Count - 1
Dim tc As TableCell = GridView1.Rows(I).Cells(1) ‘定义第二列第 I 行的单元格
If oldtc1.Text = tc.Text Then
tc.Visible = False
If oldtc1.RowSpan = 0 Then
oldtc1.RowSpan = 1
End If
oldtc1.RowSpan = oldtc1.RowSpan + 1
oldtc1.VerticalAlign = VerticalAlign.Middle
Else
oldtc1 = tc
End If
Next
End If