C# GridView 合并单元格

C# GridView 合并单元格采用什么方法好?迭代?for语句?
table:      SNAME      CNAME      GRADE
               AA              English      37
               AA              English      49
               AA              Maths        93
               AA              Maths        115
               AA              Maths        97
   
合并后:SNAME      CNAME      GRADE
                                 English      37
                                                   49
               AA                               93
                                 Maths        115

                                                  97

------------------------------------------------------------------------------------

int row=0;
for (int i = 1; i < GridView1.Rows.Count; i++)
                    {
                            if (GridView1.Rows[i].Cells[0].Text == GridView1.Rows[i - 1].Cells[0].Text)
                            {
                                GridView1.Rows[row].Cells[0].RowSpan++;
                                GridView1.Rows[i].Cells[0].Visible = false;
                            }
                            else
                            {
                                GridView1.Rows[row].Cells[0].RowSpan++;
                                row = i;
                            }
                    }
                    GridView1.Rows[row].Cells[0].RowSpan++;



--------------------------------------------------------------------------------------

  int userNameId;
    string name = "";
    int departNameId;
    string depart = "";
    int branchNameId;
    string branch = "";
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //合并用户名
            Label userName = e.Row.FindControl("lblUserName") as Label;
            if (userName.Text == name)
            {
                if (GridView1.Rows[userNameId].Cells[0].RowSpan == 0)
                {
                    GridView1.Rows[userNameId].Cells[0].RowSpan = 1;
                }
                GridView1.Rows[userNameId].Cells[0].RowSpan++;
                e.Row.Cells[0].Visible = false;
            }
            else
            {
                name = userName.Text;
                userNameId = e.Row.RowIndex;
            }
            //合并所属部门
            Label departName = e.Row.FindControl("lblDepartName") as Label;
            if (departName.Text == depart)
            {
                if (GridView1.Rows[departNameId].Cells[4].RowSpan == 0)
                {
                    GridView1.Rows[departNameId].Cells[4].RowSpan = 1;
                }
                GridView1.Rows[departNameId].Cells[4].RowSpan++;
                e.Row.Cells[4].Visible = false;
            }
            else
            {
                depart = departName.Text;
                departNameId = e.Row.RowIndex;
            }
            //合并所属机构
            Label branchName = e.Row.FindControl("lblBranchName") as Label;
            if (branchName.Text == branch)
            {
                if (GridView1.Rows[branchNameId].Cells[5].RowSpan == 0)
                {
                    GridView1.Rows[branchNameId].Cells[5].RowSpan = 1;
                }
                GridView1.Rows[branchNameId].Cells[5].RowSpan++;
                e.Row.Cells[5].Visible = false;
            }
            else
            {
                branch = branchName.Text;
                branchNameId = e.Row.RowIndex;
            }
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值