我正在尝试合并gridview的单元格,这样如果单元格(0)的文本相同,则合并行。问题是我的合并单元格并排显示而不是另一个
请在下面找到我的html和vb代码
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="GridView1_DataBound">
<Columns>
<asp:BoundField DataField="This" HeaderText="This" />
<asp:BoundField DataField="Is" HeaderText="Is" />
<asp:BoundField DataField="A" HeaderText="A" />
<asp:BoundField DataField="Test" HeaderText="Test" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
VB代码
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("this")
dt.Columns.Add("is")
dt.Columns.Add("a")
dt.Columns.Add("test")
Dim dr As DataRow = dt.NewRow()
dr("this") = "data1"
dr("is") = "data1"
dr("a") = "data1"
dr("test") = "data1"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data1"
dr("is") = "data1"
dr("a") = "data1"
dr("test") = "data1"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data1"
dr("is") = "data1"
dr("a") = "data1"
dr("test") = "data1"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data2"
dr("is") = "data2"
dr("a") = "data2"
dr("test") = "data2"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data2"
dr("is") = "data2"
dr("a") = "data2"
dr("test") = "data2"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data3"
dr("is") = "data3"
dr("a") = "data3"
dr("test") = "data3"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data4"
dr("is") = "data4"
dr("a") = "data4"
dr("test") = "data4"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data4"
dr("is") = "data4"
dr("a") = "data4"
dr("test") = "data4"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("this") = "data4"
dr("is") = "data4"
dr("a") = "data4"
dr("test") = "data4"
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub GridView1_DataBound() Handles GridView1.DataBound
For rowIndex As Integer = GridView1.Rows.Count - 2 To 0 Step -1
Dim row As GridViewRow = GridView1.Rows(rowIndex)
Dim previousRow As GridViewRow = GridView1.Rows(rowIndex + 1)
If row.Cells(0).Text = previousRow.Cells(0).Text Then
For cellIndex As Integer = 0 To row.Cells.Count - 1
If previousRow.Cells(cellIndex).RowSpan < 2 Then
row.Cells(cellIndex).RowSpan = 2
Else
row.Cells(cellIndex).RowSpan = previousRow.Cells(cellIndex).RowSpan + 1
End If
If cellIndex <> 1 AndAlso cellIndex <> 2 Then
previousRow.Cells(cellIndex).Controls.Clear()
previousRow.Cells(cellIndex).Controls.Clear()
End If
Next
'previousRow.Visible = False
End If
Next
End Sub
End Class
答案 0 :(得分:1)
您需要做的事实上是删除您正在跨越的单元格。当你给出一个单元格RowSpan = 2
时,你所进入的单元格仍在那里 - 它只会被碰到。
GridViewRow.Cells.RemoveAt(index);