GridView<asp:TemplateField>合并信息相同的行

本文介绍了一种使用C#实现的GridView控件中行合并的方法。通过遍历GridView的每一行,比较特定列的内容来决定是否进行合并,并调整了行跨度以达到视觉上的连续显示效果。

  public static void GroupItemTemplate(GridView  GridView1, int cols)
    {
     if(GridView1.Rows.Count<1||cols>GridView1.Rows[0].Cells.Count-1)
     {
         return;
    
     }
     Label lbl1 = (Label)GridView1.Rows[0].Cells[cols].FindControl("PLANNAME");
     TableCell oldTc = GridView1.Rows[0].Cells[cols];
     for (int i = 1; i < GridView1.Rows.Count; i++)
        {
            TableCell tc = GridView1.Rows[i].Cells[cols];
            Label lbl2 = (Label)GridView1.Rows[i].Cells[cols].FindControl("PLANNAME");
            if (lbl1.Text == lbl2.Text)
            {
                lbl2.Visible = false;

                if (oldTc.RowSpan == 0)
                {
                    oldTc.RowSpan = 1;

                }

                oldTc.RowSpan++;
              
            }
            else
            {

                lbl1 = lbl2;
                oldTc = tc;
            }

        }
   
    }

先将需要的值全部初始化:

行数设为0,字符串的值设为String.Empty,以及最后一个相同值的行的索引设为0

1  如果行数大于0,

       那么将需要合并的第一行第N列的值赋值给初始化的字符串对象。

       第一行第N列的行合并值设为1

2 当行数小于GridView对象的总行数时,执行

    2.1 先判断第二行的值是否等于第一行的值

          如果相等的话,那么第二行的可见属性就为false.

          并且这一行的第N列的航合并值自增1;

        

          如果不相等的话,将第i行第N列的值赋值给初始化的字符串,

         将这一行的索引赋值给初始化的整型变量

         将第i行第N列的合并行数的值设为1.

       

         

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShengGongSiGuanJianZhiBiaoList.aspx.cs" Inherits="ShengGongSiGuanJianZhiBiaoList" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="../css/css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script> <style type="text/css"> #head { height: auto; width: 900px; margin-left: auto; margin-right: auto; } #table01 { border-left: #99bbe8 solid 1px; border-top: #99bbe8 solid 1px; } #table01 th, #table01 td { border-right: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; } #table01 th { background: #f5fafa; color: #047E6C; font-weight: bold; } #no_border { border-left: 0px; border-right: 0px; border-bottom: 0px; } #cccc { border-right: #99bbe8 solid 1px; border-left: #99bbe8 solid 1px; border-bottom: #99bbe8 solid 1px; width: 2%; font-size: 12px; height: 30px; text-align: center; background: #f5fafa; } </style> <script type="text/javascript"> function validate() { $("input[name^='yi_pingfenbiaozhun_']").attr("name", "yi_pingfenbiaozhun_"); $("input[name^='ba_pingfen_']").attr("name", "ba_pingfen_"); return true; } </script> </head> <body> <form id="form1" runat="server"> <center> <div> <br /><br /> <table style="width: 90%; border-top: #99bbe8 solid 1px;" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <th id="cccc" colspan="11" class="auto-style1"><span style="color: black; font-size: x-large"><asp:DropDownList ID="dlyear" runat="server"> <asp:ListItem Value="2025">2025</asp:ListItem> <asp:ListItem Value="2026">2026</asp:ListItem> <asp:ListItem Value="2027">2027</asp:ListItem> <asp:ListItem Value="2028">2028</asp:ListItem> </asp:DropDownList>年<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> <asp:ListItem Value="3">3</asp:ListItem> <asp:ListItem Value="4">4</asp:ListItem> <asp:ListItem Value="5">5</asp:ListItem> <asp:ListItem Value="6">6</asp:ListItem> <asp:ListItem Value="7">7</asp:ListItem> <asp:ListItem Value="8">8</asp:ListItem> <asp:ListItem Value="9">9</asp:ListItem> <asp:ListItem Value="10">10</asp:ListItem> <asp:ListItem Value="11">11</asp:ListItem> <asp:ListItem Value="12">12</asp:ListItem> </asp:DropDownList>省公司关键业绩指标排名</span></th> </tr> </table> <br /> <div> <asp:Button ID="Button2" runat="server" CssClass="Button" Text="查 询" OnClick="Button2_Click"></asp:Button> <asp:Button ID="Button1" runat="server" OnClientClick="return validate()" CssClass="Button" Text="保 存" OnClick="Button1_Click"></asp:Button> </div> <br /> <table id="table01" style="width: 100%" cellpadding="0" cellspacing="0"> <tr> <th>序号</th> <th>考核指标</th> <th>排名</th> <th>一级指标牵头部门</th> <th>一级指标责任部门名称</th> <th>二级指标</th> <th>二级指标责任部门</th> </tr> <%if (dtbm.Rows.Count > 0) { foreach (DataRow bmRow in dtbm.Rows) { string firstId = bmRow["uid"].ToString(); // 1. 获取一级责任部门(多个) DataRow[] firstDeptRows = dtbminfo.Select("一级Id='" + firstId + "'"); int firstDeptCount = firstDeptRows.Length; // 一级责任部门数量 // 2. 计算二级和三级指标的总数 int secondThirdTotalRows = 0; DataRow[] secondRows = dtsan.Select("一级Id='" + firstId + "'"); foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级自身 + 三级数 secondThirdTotalRows += secondTotalRows; // 累加二级和三级的总数 } // 3. 一级指标核心列的合并数 = 一级责任部门数量 + 二级三级总数 int firstTotalRows = firstDeptCount + secondThirdTotalRows; %> <!-- 一级指标(核心列合并所有子) --> <tr> <td rowspan="<%= firstTotalRows %>"><%= bmRow["序号"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["考核指标"].ToString() %></td> <td rowspan="<%= firstTotalRows %>"> <input type="hidden" id="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" name="yi_pingfenbiaozhun_<%=bmRow["uid"].ToString()%>" value="<%=bmRow["uid"].ToString() %>" style="width: 98%" /> <input type="text" style="width:20px" value="" name="ba_pingfen_<%= bmRow["uid"].ToString() %>" /> </td> <td rowspan="<%= firstTotalRows %>"><%= bmRow["一级指标牵头部门"].ToString() %></td> <td></td> <!-- 一级责任部门列留空,后续分填充 --> <!-- 处理二级指标(如果有) --> <%if (secondRows.Length > 0) { bool isFirstSecond = true; foreach (DataRow sanRow in secondRows) { string secondId = sanRow["uid"].ToString(); DataRow[] thirdRows = dtwu.Select("二级Id='" + secondId + "'"); int secondTotalRows = 1 + thirdRows.Length; // 二级合并数 %> <!-- 二级指标 --> <%if (isFirstSecond) { isFirstSecond = false; %> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> <%} else { %> <tr> <td rowspan="<%= secondTotalRows %>"><%= sanRow["二级指标"].ToString() %></td> </tr> <%}%> <!-- 处理三级指标(二级责任部门) --> <%if (thirdRows.Length > 0) { foreach (DataRow wuRow in thirdRows) { %> <tr> <td><%= wuRow["二级指标责任部门名称"].ToString() %></td> </tr> <%} } else { %> <tr> <td></td> </tr> <%}%> <%} } else { %> <td></td> <td></td> <%}%> </tr> <!-- 一级责任部门分显示(在一级指标之后、二级指标之前) --> <%foreach (DataRow deptRow in firstDeptRows) { string deptName = deptRow["一级指标责任部门名称"].ToString(); if (!string.IsNullOrEmpty(deptName)) { %> <tr> <td><%= deptName %></td> <!-- 第5列:一级责任部门名称 --> <td></td> <!-- 第6列:二级指标(留空,后续二级填充) --> <td></td> <!-- 第7列:二级责任部门(留空) --> </tr> <% } }%> <% } } else { %> <tr> <td colspan="7" style="height:30px;">暂无数据</td> </tr> <%}%> </table> </div> </center> </form> </body> </html>
07-19
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>简洁二级表头(对齐版)</title> <style type="text/css"> .gridview { width: 100%; } .inner-table { width: 100%; border-collapse: collapse; } .inner-table th, .inner-table td { border: 1px solid #000; padding: 4px; text-align: center; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" CssClass="gridview" AutoGenerateColumns="False" ShowHeader="true"> <Columns> <asp:TemplateField> <HeaderTemplate> <table class="inner-table"> <tr> <!-- 基础列:3列(跨2) --> <th rowspan="2">Plant</th> <th rowspan="2">Dept</th> <th rowspan="2">Area</th> <!-- From HR:3列 --> <th colspan="3">From HR(2025-11-13)</th> <!-- Loading:6列 --> <th colspan="6">Loading</th> <!-- HC:6列 --> <th colspan="6">HC</th> <!-- OT:6列 --> <th colspan="6">OT</th> </tr> <tr> <!-- From HR 子列(3列) --> <th>Contract_worker</th> <th>Leased_worker</th> <th>TotalWorker</th> <!-- Loading 子列(6列) --> <th>2025-11</th> <th>2025-12</th> <th>2026-01</th> <th>2026-02</th> <th>2026-03</th> <th>2026-04</th> <!-- HC 子列(6列) --> <th>2025-11</th> <th>2025-12</th> <th>2026-01</th> <th>2026-02</th> <th>2026-03</th> <th>2026-04</th> <!-- OT 子列(6列) --> <th>2025-11</th> <th>2025-12</th> <th>2026-01</th> <th>2026-02</th> <th>2026-03</th> <th>2026-04</th> </tr> </table> </HeaderTemplate> <ItemTemplate> <table class="inner-table"> <tr> <!-- 基础列(3列):与表头对应 --> <td><%# Eval("Plant") %></td> <td><%# Eval("Dept") %></td> <td><%# Eval("Area") %></td> <!-- From HR 数据(3列):与表头子列顺序一致 --> <td><%# Eval("Contract_worker") %></td> <td><%# Eval("Leased_worker") %></td> <td><%# Eval("TotalWorker") %></td> <!-- Loading 数据(6列):与时间顺序一致 --> <td><%# Eval("Loading_202511", "{0:P1}") %></td> <td><%# Eval("Loading_202512", "{0:P1}") %></td> <td><%# Eval("Loading_202601", "{0:P1}") %></td> <td><%# Eval("Loading_202602", "{0:P1}") %></td> <td><%# Eval("Loading_202603", "{0:P1}") %></td> <td><%# Eval("Loading_202604", "{0:P1}") %></td> <!-- HC 数据(6列):与时间顺序一致 --> <td><%# Eval("HC_202511") %></td> <td><%# Eval("HC_202512") %></td> <td><%# Eval("HC_202601") %></td> <td><%# Eval("HC_202602") %></td> <td><%# Eval("HC_202603") %></td> <td><%# Eval("HC_202604") %></td> <!-- OT 数据(6列):与时间顺序一致 --> <td><%# Eval("OT_202511") %></td> <td><%# Eval("OT_202512") %></td> <td><%# Eval("OT_202601") %></td> <td><%# Eval("OT_202602") %></td> <td><%# Eval("OT_202603") %></td> <td><%# Eval("OT_202604") %></td> </tr> </table> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
最新发布
11-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值