合并DataGrid中的单元格

DataGrid中的每一个单元格为TableCell对象,通过设置其“ColumnSpan”与“RowSpan”属性,可以达到合并单元格的目的。

ExpandedBlockStart.gif 代码
         ///   <summary>
        
///  合并单元格 合并行中的几列(如果开始列后面列的内容为空,则将其也开始列合并)
        
///   </summary>
        
///   <param name="dg"> DataGrid </param>
        
///   <param name="sCol"> 开始列 </param>
        
///   <param name="eCol"> 结束列 </param>
         public   static   void  GroupCols(SmartDataGrid dg,  int  sCol,  int  eCol)
        {
            
for  ( int  row  =   0 ; row  <  dg.Items.Count; row ++ )
            {
                TableCell oldTc 
=  dg.Items[row].Cells[sCol];
                oldTc.Style.Add(
" text-align " " center " );
                oldTc.VerticalAlign 
=  VerticalAlign.Middle;

                
for  ( int  i  =   1 ; i  <=  eCol  -  sCol; i ++ )
                {
                    TableCell tc 
=  dg.Items[row].Cells[i  +  sCol];
                    tc.Style.Add(
" text-align " " center " );
                    tc.VerticalAlign 
=  VerticalAlign.Middle;
                    Label lbl 
=  tc.FindControl( " lblAgendaTime " as  Label;
                    
if  ( string .IsNullOrEmpty(lbl.Text))
                    {
                        tc.Visible 
=   false ;
                        
if  (oldTc.ColumnSpan  ==   0 )
                        {
                            oldTc.ColumnSpan 
=   1 ;
                        }
                        oldTc.ColumnSpan
++ ;
                    }
                }
            }
        }

        
///   <summary>
        
///  合并单元格 合并某一列中的值相同的行
        
///   </summary>
        
///   <param name="dg"> DataGrid </param>
        
///   <param name="col"> </param>
         public   static   void  GroupRows(SmartDataGrid dg,  int  col)
        {
            
if  (dg.Items.Count  <   1   ||  col  >  dg.Columns.Count  -   1 )
            {
                
return ;
            }
            
int  sRow  =   0 ;
            
int  eRow  =  dg.Items.Count  -   1 ;

            
while  (sRow  <  eRow)
            {
                TableCell oldTc 
=  dg.Items[sRow].Cells[col];
                Label oldLbl 
=  oldTc.FindControl( " lblAgendaDate " as  Label;
                
int  i  =   1 ;
                
for  (i = 1 ; i  <=  eRow  -  sRow; i ++ )
                {
                    TableCell tc 
=  dg.Items[sRow  +  i].Cells[col];
                    Label lbl 
=  tc.FindControl( " lblAgendaDate " as  Label;
                    
if  (oldLbl.Text  ==  lbl.Text)
                    {
                        tc.Visible 
=   false ;
                        
if  (oldTc.RowSpan  ==   0 )
                        {
                            oldTc.RowSpan 
=   1 ;
                        }
                        oldTc.RowSpan
++ ;
                    }
                    
else
                    {
                        i
++ ;
                        
break ;
                    }
                }

                sRow 
=  sRow  +  i  -   1 ;
            }
        }


 

转载于:https://www.cnblogs.com/yyniuznyin/archive/2009/12/05/1617595.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值