asp:GridView 合并相同数据项行
前台:
<asp:GridView ID="gvInfo" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" HorizontalAlign="left" EmptyDataText="无" OnDataBound="GridView1_DataBound" Width="100%"> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <Columns> <asp:TemplateField HeaderText="序号"> <ItemTemplate> <%#Container.DataItemIndex+1%> </ItemTemplate> <ItemStyle Width="5%" HorizontalAlign="Center" /> </asp:TemplateField><asp:BoundField DataField="ShowDay" HeaderText="日期"> <ItemStyle Width="10%" /> </asp:BoundField> <asp:BoundField DataField="ShowTime" HeaderText="时间"> <ItemStyle Width="20%" /> </asp:BoundField> <asp:BoundField DataField="Place" HeaderText="地点"> <ItemStyle Width="15%" /> </asp:BoundField> <asp:BoundField DataField="Content" HeaderText="内容"> <ItemStyle Width="25%" /> </asp:BoundField> <asp:BoundField DataField="Persons" HeaderText="参加人员"> <ItemStyle Width="15%" /> </asp:BoundField> </Columns> <EditRowStyle BackColor="#2461BF" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#cadef4" Font-Bold="True" ForeColor="#3b6798" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView
后台:
protected void Page_Load(object sender, EventArgs e){gvInfo.DataSource = List<T>;gvInfo.DataBind();}protected void GridView1_DataBound(object sender, EventArgs e){int row = 0;for (int i = 1; i < gvInfo.Rows.Count; i++){if (gvInfo.Rows[i].Cells[1].Text == gvInfo.Rows[i - 1].Cells[1].Text){if (gvInfo.Rows[row].Cells[1].RowSpan == 0) gvInfo.Rows[row].Cells[1].RowSpan++;gvInfo.Rows[row].Cells[1].RowSpan++;gvInfo.Rows[i].Cells[1].Visible = false;}else{row = i;}}}