Asp.Net 中实现DataGrid指定列的聚合功能,支持模板列聚合

  /// <summary>
  /// 纵向合并指定列的相同文本单元格  
  /// </summary>
  /// <usage>
  /// MergeSameCellAtTextColumn(DataGridInstance,columnIndex);
  /// </usage>
  /// <param name="curGrid"></param>
  /// <param name="columnIndex"></param>
  private void MergeSameCellAtTextColumn(DataGrid curGrid,int columnIndex)
  {
   DataGridItem  curItem = null;
   DataGridItem  nextItem = null;
   for (int r = 0; r < curGrid.Items.Count; r++)
   {
    ///当前单元格
    curItem = curGrid.Items[r];
    ///其下单元格
    if(r + 1 < curGrid.Items.Count)
     nextItem = curGrid.Items[r + 1];
    else
     nextItem = null;
    string curValue = curItem == null ? string.Empty:curItem.Cells[columnIndex].Text;
    string nextValue = nextItem == null ? string.Empty:nextItem.Cells[columnIndex].Text;
    ///如果单元格内容相同
    if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
    {
     
     ///设置其下单元格不可见
     if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
     ///如果当前单元格可见,则将其RowSpan + 1
     if(curItem.Cells[columnIndex].Visible == true)
     {
      curItem.Cells[columnIndex].RowSpan += 1;      
     }
      ///如果当前单元格不可见,则直接回溯到其上第一个可见单元格为止
     else if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///将其上第一个可见单元格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
    } 
    ///如果单元格内容不同
    else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
    {
     ///判断是否是临界行:上面的一行或多行与下面的一行或多行的对应单元格内容不同
     if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///将其上第一个可见单元格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }     
     else
     {
      ///非临界
     }
    }
    ///数据列表的最后一行
    else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
    {
     int preRowIndex = r;
     while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
     {
      preRowIndex -= 1;
     }
     ///将其上第一个可见单元格的RowSpan + 1
     curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
    }
   }
   
  } 
  /// <summary>
  /// 纵向合并指定列的相同模板单元格
  /// </summary>
  /// <usage>
  /// MergeSameCellAtTemplateColumn(DataGridInstance,"TemplateControlId",columnIndex);
  /// </usage>
  /// <param name="curGrid"></param>
  /// <param name="columnIndex"></param>
  /// <param name="controlName"></param>
  private void MergeSameCellAtTemplateColumn(DataGrid curGrid,int columnIndex,string controlName)
  {
   DataGridItem  curItem = null;
   DataGridItem  nextItem = null;
   for (int r = 0; r < curGrid.Items.Count; r++)
   {
    ///当前单元格
    curItem = curGrid.Items[r];
    ///其下单元格
    if(r + 1 < curGrid.Items.Count)
     nextItem = curGrid.Items[r + 1];
    else
     nextItem = null;
    ///调用时注意修改此处的模板控件类型和模板控件获取值的方法
    string curValue = curItem == null ? string.Empty:((ListBox)curItem.FindControl(controlName)).SelectedText;
    string nextValue = nextItem == null ? string.Empty:((ListBox)nextItem.FindControl(controlName)).SelectedText;
    ///如果单元格内容相同
    if(curValue != string.Empty && nextValue != string.Empty && curValue == nextValue)
    {
     ///设置其下单元格不可见
     if(nextValue != null)nextItem.Cells[columnIndex].Visible = false;
     ///如果当前单元格可见,则将其RowSpan + 1
     if(curItem.Cells[columnIndex].Visible == true)
     {
      curItem.Cells[columnIndex].RowSpan += 1;      
     }
      ///如果当前单元格不可见,则直接回溯到其上第一个可见单元格为止
     else if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///将其上第一个可见单元格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
    } 
    ///如果单元格内容不同
    else if(curValue != string.Empty && nextValue != string.Empty && curValue != nextValue)
    {
     ///判断是否是临界行:上面的一行或多行与下面的一行或多行的对应单元格内容不同
     if(curItem.Cells[columnIndex].Visible == false)
     {
      int preRowIndex = r;
      while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
      {
       preRowIndex -= 1;
      }
      ///将其上第一个可见单元格的RowSpan + 1
      curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
     }
     else
     {
      ///非临界
     }
    }
    ///数据列表的最后一行
    else if(r == curGrid.Items.Count - 1 && curItem.Cells[columnIndex].Visible == false)
    {
     int preRowIndex = r;
     while(preRowIndex >= 0 && curGrid.Items[preRowIndex].Cells[columnIndex].Visible == false)
     {
      preRowIndex -= 1;
     }
     ///将其上第一个可见单元格的RowSpan + 1
     curGrid.Items[preRowIndex].Cells[columnIndex].RowSpan += 1;
    }
   }
   
  } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值