前段时间在编写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
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
//这里就是解决方案 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) { case DataControlRowType.Header: //第一行表头 TableCellCollection tcHeader = e.Row.Cells; tcHeader.Clear(); tcHeader.Add(new TableHeaderCell()); tcHeader[0].Attributes.Add("rowspan", "3"); //跨Row tcHeader[0].Attributes.Add("bgcolor", "white"); tcHeader[0].Text = ""; tcHeader.Add(new TableHeaderCell()); //tcHeader[1].Attributes.Add("bgcolor", "Red"); tcHeader[1].Attributes.Add("colspan", "6"); //跨Column tcHeader[1].Text = "全部信息</th></tr><tr>";
//第二行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[2].Text = "身份证号码"; tcHeader.Add(new TableHeaderCell()); tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue"); tcHeader[3].Attributes.Add("colspan", "2"); tcHeader[3].Text = "基本信息"; tcHeader.Add(new TableHeaderCell()); tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen"); tcHeader[4].Text = "福利"; tcHeader.Add(new TableHeaderCell()); tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue"); tcHeader[5].Attributes.Add("colspan", "2"); tcHeader[5].Text = "联系方式</th></tr><tr>";
//第三行表头 tcHeader.Add(new TableHeaderCell()); tcHeader[6].Attributes.Add("bgcolor", "Khaki"); tcHeader[6].Text = "身份证号码"; tcHeader.Add(new TableHeaderCell()); tcHeader[7].Attributes.Add("bgcolor", "Khaki"); tcHeader[7].Text = "姓名"; tcHeader.Add(new TableHeaderCell()); tcHeader[8].Attributes.Add("bgcolor", "Khaki"); tcHeader[8].Text = "出生日期"; tcHeader.Add(new TableHeaderCell()); tcHeader[9].Attributes.Add("bgcolor", "Khaki"); tcHeader[9].Text = "薪水"; tcHeader.Add(new TableHeaderCell()); tcHeader[10].Attributes.Add("bgcolor", "Khaki"); tcHeader[10].Text = "家庭住址"; tcHeader.Add(new TableHeaderCell()); tcHeader[11].Attributes.Add("bgcolor", "Khaki"); tcHeader[11].Text = "邮政编码"; break; } }}
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>GridView合并多重表头表头 清清月儿http://blog.csdn.net/21aspnet </title></head><body > <form id="form1" runat="server"> <div > <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Size="12px" OnRowCreated="GridView1_RowCreated" > <FooterStyle BackColor="White" ForeColor="#000066" /> <Columns> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:BoundField DataField="身份证号码" HeaderText="编号" ReadOnly="True" /> <asp:BoundField DataField="姓名" HeaderText="姓名" /> <asp:BoundField DataField="出生日期" HeaderText="邮政编码" /> <asp:BoundField DataField="起薪" HeaderText="起薪" /> <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" /> <asp:BoundField DataField="邮政编码" HeaderText="邮政编码" /> </Columns> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" CssClass="ms-formlabel DataGridFixedHeader"/> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> </asp:GridView> </div>
</form></body></html>