zoukankan      html  css  js  c++  java
  • asp.net中合并DataGrid行

     

    例如:

    序号            名称      计量单位
    一、工业经济    aaa       万元
    一、工业经济    bbb       个

    合并为:

    序号            名称      计量单位
    一、工业经济    aaa       万元
                    bbb       个
    只是将第一列的多行合并为一行,怎么实现?谢谢大家了先!

    实现方法:
    把笨狼哥的改成这样试试:
    在.aspx页面,<asp:datagrid>中用
    OnPreRender="fDGrid_PreRender">

    在.cs文件:

     //合并相同的单元格
      public void fDGrid_PreRender(object sender, System.EventArgs e)
      {
       if(this.fDGrid.Items.Count <=1)
       {
        return;
       }
       col=0;
        TableCell oldtc = this.fDGrid.Items[0].Cells[col];
        for(int i=1;i<this.fDGrid.Items.Count;i++)
        {
         TableCell tc = this.fDGrid.Items[i].Cells[col];
         if(tc.Text == oldtc.Text)
         {
          tc.Visible = false;
          if(oldtc.RowSpan == 0)
          {
           oldtc.RowSpan = 1;
          }
          oldtc.RowSpan = oldtc.RowSpan +1;
          oldtc.VerticalAlign = VerticalAlign.Middle;
         }
         else
         {
          oldtc = tc;
         }
        }
      }

    当然,还可以用ItemDataBound事件来处理。具体细节如下

    在.cs文件中的
    InitializeComponent方法中加入:
      
     this.dgContacts.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgContacts_ItemDataBound);

    在.cs文件中的Page_Load中加入:
    if (!Page.IsPostBack )
    {
    lastIndex=0;
    }

    其中dgContacts为DataGrid的名字

    再在 .cs文件中加入下面的代码:
             int lastIndex;
     protected void dgContacts_ItemDataBound(object source,
       System.Web.UI.WebControls.DataGridItemEventArgs e)
      {
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
        string isManager = (string)DataBinder.Eval(e.Item.DataItem, "序号");
        int inn=e.Item.ItemIndex;
        TableCell c;
                                         int col=0;
        if (inn>0)
        {
         if(dgContacts.Items[lastIndex].Cells[col].Text==isManager)
         {
          
          c=new TableCell();
          c=e.Item.Cells[col];
          if(dgContacts.Items[lastIndex].Cells[col].RowSpan==0)
               dgContacts.Items[lastIndex].Cells[col].RowSpan=1;

          dgContacts.Items[lastIndex].Cells[col].RowSpan+=1;
          
          Response.Write(dgContacts.Items[lastIndex].Cells[col].RowSpan);
          e.Item.Cells.Remove(c);
          
         }
         else
         {
          e.Item.Cells[col].Text=isManager;
          lastIndex=e.Item.ItemIndex;
         }
        }
        else
        {
         e.Item.Cells[col].Text=isManager;
        }
        
       }
      }

    两种方法都可以,但是还是第一中方法好,通用性也强,第二种方法如果稍加修改,应该也可以。可能还有其他方法。具体用那种方法不重要,重要的是如何灵活应用基本的知识解决复杂的问题。

  • 相关阅读:
    值币转换编程总结
    打印沙漏编程总结
    Leetcode每日一题 面试题 17.21. 直方图的水量
    VS2017-OPENGL配置glfw+glad
    OpenGL(二) 绘制一个三角形
    OpenGL(一) 渲染循环创建窗口
    Leetcode每日一题 1006.笨阶乘
    Leetcode每日一题 90.子集 II
    Leetcode每日一题 190.颠倒二进制位
    Leetcode 第243场周赛 创新奇智&力扣
  • 原文地址:https://www.cnblogs.com/Fooo/p/519691.html
Copyright © 2011-2022 走看看