日期:2014-05-17  浏览次数:20904 次

asp.net gridview 合并标题和列单元格


??????? //合併gridview表頭
??????? #region
??????? public static void GroupHeaderRow(GridView GridView1, int rows, int sCol, int eCol)
??????? {
??????????? TableCell oldTc = GridView1.HeaderRow.Cells[sCol];
??????????? for (int i = 1; i < eCol - sCol; i++)
??????????? {
??????????????? TableCell tc = GridView1.HeaderRow.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;
??????????????????? //tc.Text = "";
          if (oldTc.RowSpan == 0)
          {
            oldTc.RowSpan = 1;
          }
          oldTc.RowSpan++;
          oldTc.VerticalAlign = VerticalAlign.Middle;
        }
        else
        {
          oldTc = tc;
        }
      }
    }
    #endregion

??? #region
??? /// <summary>
??? /// 合并GridView中某列相同信息的行(单元格)
??? /// </summary>
??? /// <param name="GridView1"></param>
??? /// <param name="cellNum"></param>
??? public static void GroupCol(GridView GridView1, int beforcols,int cols)
??? {
??????? if (GridView1.Rows.Count < 1 || cols > GridView1.Rows[0].Cells.Count - 1)
??????? {
??????????? return;
??????? }
??????? TableCell oldTc = GridView1.Rows[0].Cells[cols];
??????? TableCell boldTc = GridView1.Rows[0].Cells[beforcols];
??????? for (int i = 1; i < GridView1.Rows.Count; i++)
??????? {
??????????? TableCell tc = GridView1.Rows[i].Cells[cols];
??????????? TableCell btc = GridView1.Rows[i].Cells[beforcols];
??????????? if (boldTc.Text == btc.Text)//如果前一項相同
??????????? {
??????????????? if (oldTc.Text == tc.Text)//本項相同 合併
??????????????? {
??????????????????? tc.Visible = false;
??????????????????? //tc.Text = "";
??????????????????? if (oldTc.RowSpan == 0)
??????????????????? {
??????????????????????? oldTc.RowSpan = 1;
??????????????????? }
??????????????????? oldTc.RowSpan++;
?????????????????????????
??????????????????? oldTc.VerticalAlign = VerticalAlign.Middle;
??????????????? }
??????????????? else//本項不同
??????????????? {
??????????????????? oldTc = tc;
??????????????? }
??????????? }
??????????? else//前一項不同
??????????? {???????????????
??????????????????? oldTc = tc;
??????????? }????
??????? }
??? }
??? #endregion