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

代码
        /// <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;
            }
        }


 

posted on 2009-12-05 16:29  znyin  阅读(215)  评论(0编辑  收藏  举报