欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GridView单元格合并

程序员文章站 2023-11-14 16:32:58
#region 合并单元格 合并某一行的所有列   ///      ///  合并gridview中某行相同信息的行(单元格)   /// ...
#region 合并单元格 合并某一行的所有列
  ///  <summary> 
  ///  合并gridview中某行相同信息的行(单元格)
  ///  </summary> 
  ///  <param  name="gridview1">gridview对象</param> 
  ///  <param  name="cellnum">需要合并的行</param>
  public static void grouprow(gridview gridview1, int rows)
  {
  tablecell oldtc = gridview1.rows[rows].cells[0];
  for (int i = 1; i < gridview1.rows[rows].cells.count; i++)
  {
  tablecell tc = gridview1.rows[rows].cells[i];  //cells[0]就是你要合并的列
  if (oldtc.text == tc.text)
  {
  tc.visible = false;
  if (oldtc.columnspan == 0)
  {
  oldtc.columnspan = 1;
  }
  oldtc.columnspan++;
  oldtc.verticalalign = verticalalign.middle;
        }
        else
        {
          oldtc = tc;
        }
      }
    }
    #endregion
    #region 合并单元格 合并一行中的几列
    /// <summary>
    /// 合并单元格 合并一行中的几列
    /// </summary>
    /// <param name="gridview1">gridview id</param>
    /// <param name="rows">行</param>
    /// <param name="scol">开始列</param>
    /// <param name="ecol">结束列</param>
    public static void grouprow(gridview gridview1, int rows,int scol,int ecol)
    {
       tablecell oldtc = gridview1.rows[rows].cells[scol];
      for (int i = 1; i < ecol - scol; i++)
      {
        tablecell tc = gridview1.rows[rows].cells[i + scol];  //cells[0]就是你要合并的列
        tc.visible = false;
        if (oldtc.columnspan == 0)
        {
          oldtc.columnspan = 1;
        }
        oldtc.columnspan++;
        oldtc.verticalalign = verticalalign.middle;
      }
    }
    #endregion
    #region 合并单元格 合并某一列所有行
    /// <summary>
    /// 合并gridview中某列相同信息的行(单元格)
    /// </summary>
    /// <param name="gridview1"></param>
    /// <param name="cellnum"></param>
    public static void groupcol(gridview gridview1, int cols)
    {
      if (gridview1.rows.count < 1 || cols > gridview1.rows[0].cells.count - 1)
      {
        return;
      }
      tablecell oldtc = gridview1.rows[0].cells[cols];
      for (int i = 1; i < gridview1.rows.count; i++)
      {
        tablecell tc = gridview1.rows[i].cells[cols];
        if (oldtc.text == tc.text)
        {
          tc.visible = false;
          if (oldtc.rowspan == 0)
          {
            oldtc.rowspan = 1;
          }
          oldtc.rowspan++;
          oldtc.verticalalign = verticalalign.middle;
        }
        else
        {
          oldtc = tc;
        }
      }
    }
    #endregion
    #region 合并单元格 合并某一列中的某些行
    /// <summary>
    /// 合并单元格 合并某一列中的某些行
    /// </summary>
    /// <param name="gridview1">gridview id</param>
    /// <param name="cellnum">列</param>
    /// <param name="srow">开始行</param>
    /// <param name="erow">结束列</param>
    public static void groupcol(gridview gridview1, int cols,int srow,int erow)
    {
      if (gridview1.rows.count < 1 || cols > gridview1.columns.count - 1)
      {
        return;
      }
      tablecell oldtc = gridview1.rows[srow].cells[cols];
      for (int i = 1; i < erow - srow; i++)
      {
        tablecell tc = gridview1.rows[srow + i].cells[cols]; 
        tc.visible = false;
        if (oldtc.rowspan == 0)
        {
          oldtc.rowspan = 1;
        }
        oldtc.rowspan++;
        oldtc.verticalalign = verticalalign.middle;
      }
    }
    #endregion