You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在按类别分组的GridView中实现单元格跨行合并?

ASP.NET GridView 实现类别单元格跨行合并(按CAT_DESC分组)

要实现按CAT_DESC列分组的跨行合并,核心是在GridView的RowDataBound事件中逐行对比类别值,动态调整单元格的RowSpan属性并隐藏重复的类别单元格。以下是完整实现步骤:

1. 修改GridView的ASPX标记

给GridView添加OnRowDataBound事件绑定,确保后台能处理行绑定逻辑:

<asp:GridView ID="gvResults" ClientIDMode="Static" runat="server"
    Caption='<div class="alert alert-info primary mb-0" style="color: white;">ترخيص</div>'
    CaptionAlign="Top"
    ShowFooter="False"
    RowStyle-CssClass="record"
    border="0"
    BorderColor="White"
    CellPadding="0"
    CellSpacing="2"
    CssClass="table table-bordered"
    PageSize="5"
    Width="100%"
    AutoGenerateColumns="false"
    OnRowDataBound="gvResults_RowDataBound"> <!-- 添加行绑定事件 -->
    
    <RowStyle HorizontalAlign="Center" CssClass="table-striped" />
    
    <HeaderStyle BackColor="#800080" ForeColor="White" CssClass="text-white header-center" HorizontalAlign="Center" />
    
    <Columns>
        <asp:BoundField DataField="CAT_DESC" HeaderText="الفئه" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center" />
        <asp:BoundField DataField="ACT_DESC" HeaderText="النشاط" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center" />
   
    </Columns>
</asp:GridView>

2. 后台C#代码实现合并逻辑

在页面后台定义私有变量记录上一行的类别值和合并行数,然后在RowDataBound事件中处理单元格合并:

// 记录上一行的类别值与当前合并行数
private string _prevCatDesc = string.Empty;
private int _mergeRowCount = 1;

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // 仅处理数据行,跳过表头、页脚等非数据行
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView rowView = (DataRowView)e.Row.DataItem;
        string currentCatDesc = rowView["CAT_DESC"].ToString();

        // 对比当前行与上一行的类别值
        if (currentCatDesc == _prevCatDesc)
        {
            _mergeRowCount++;
            // 隐藏当前行的类别单元格
            e.Row.Cells[0].Visible = false;
            // 更新上一行类别单元格的跨行数
            GridViewRow prevRow = gvResults.Rows[e.Row.RowIndex - 1];
            prevRow.Cells[0].RowSpan = _mergeRowCount;
        }
        else
        {
            // 切换到新类别时重置计数器与记录值
            _mergeRowCount = 1;
            _prevCatDesc = currentCatDesc;
        }
    }
}

3. 关键补充说明

  • 数据源排序:必须确保数据源已按CAT_DESC排序,否则合并逻辑会失效。绑定前可添加排序代码:
    response.ds.Tables["P_REGISTER_ACTS"].DefaultView.Sort = "CAT_DESC ASC";
    gvResults.DataSource = response.ds.Tables["P_REGISTER_ACTS"].DefaultView;
    
  • 分页处理:如果GridView启用分页,需在PageIndexChanging事件中重置记录变量,避免分页后合并逻辑混乱:
    protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvResults.PageIndex = e.NewPageIndex;
        _prevCatDesc = string.Empty;
        _mergeRowCount = 1;
        // 重新绑定数据源
        gvResults.DataSource = response.ds.Tables["P_REGISTER_ACTS"].DefaultView;
        gvResults.DataBind();
    }
    

完成以上修改后,即可实现你需要的效果:同一个类别的单元格跨行合并,活动列保持每行独立显示。

内容的提问来源于stack exchange,提问作者ahmed salah abd elaziz elbarba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:28:28