技不如人

Welcome to Rickel's blog.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

在DataGrid中合并单元格行

Posted on 2005-01-18 08:46  Rickel  阅读(4119)  评论(4编辑  收藏  举报
最近在做项目的时候经常遇到要合并单元格的情况,发现这个东西.net中并没有现成的方法,研究了一下,总结了两种方法。
这个主要都是行合并的,有以下两种情况
1、多行合并为一行,其中将某个或某几个单元格的内容合并起来
     原始样式:
            column1     column2
                1                    a
                1                    b
                1                    c
                1                    d
                2                    q
                2                    w
                2                    e
      需要效果:
            column1        column2
                1                    a\b\c\d
                2                    q\w\e
      代        码: 
                 

     
private void DataGrid_CaseTeamList_Bind() 
        

            取得数据部分代码省略 
            dsSpeTeam 
= o.GetData(strSQL).Tables[0].DefaultView; 
            DataSet dsSpeL 
= o.GetData(strSQLSpe); 
            DataGrid_CaseTeamList.ItemCreated 
+=new DataGridItemEventHandler(DataGrid_CaseTeamList_ItemCreated); 
            DataGrid_CaseTeamList.DataSource 
= dsSpeL.Tables[0].DefaultView; 
            DataGrid_CaseTeamList.DataBind(); 
        }
 
 
        
private void DataGrid_CaseTeamList_ItemCreated(object sender, DataGridItemEventArgs e) 
        

            DataGridItem item 
= e.Item; 
            
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
            

                DataRowView row 
= (DataRowView)e.Item.DataItem; 
                
//set the filter condition 
                string sSpecialist = row["column1"].ToString(); 
                dsSpeTeam.RowFilter 
= string.Format(" column1 = '{0}'", sSpecialist.Replace("'""''")); 
                 
                 
//----------------------------------------------------------
                
// 若要将checkbox 合并放入,可使用下面代码 代替以下部分
                 
//    CheckBoxList boxList = new CheckBoxList();
                
//    boxList.RepeatColumns  = 2;
                
//    boxList.RepeatDirection = RepeatDirection.Horizontal;
                
//    boxList.DataSource = dsSpeTeam;
                
//    boxList.DataTextField = "Team";
                
//    boxList.DataValueField = "Team";
                
//    PlaceHolder holder = (PlaceHolder) e.Item.FindControl("TeamArray");
                
//    holder.Controls.Add(boxList);
                
//----------------------------------------------------------
                 DataRowView row1 = null
                IEnumerator Enum 
= dsSpeTeam.GetEnumerator(); 
                
string strTeam= ""
                
while (Enum.MoveNext()) 
                

                    row1 
= (DataRowView) Enum.Current; 
                    strTeam 
= strTeam + row1["column2"+ "/"
                }
 
                Label lbl 
= new Label(); 
                
if (strTeam.Length > 0
                

                    lbl.Text 
= strTeam.Substring(0,strTeam.Length-1); 
                }
 
                
else 
                

                    lbl.Text 
= " "
                }
 
                lbl.Style.Add(
"padding-left","5px"); 
                
//在datagrid中,合并目标列放一个PlaceHolder控件 
                
//------------------------------------------------------------
                PlaceHolder holder = (PlaceHolder) e.Item.FindControl("column2Value"); 
                holder.Controls.Add(lbl); 
            }
 
        }
 

2、将Datagrid中的某些相同的单元格合并
        原始样式:    
                Column1            Column2
                    1                                a
                    1                                b
                    1                                c
                     2                                x
                     2                                y
                      2                               z
        需要效果:
                Column1            Column2
                                                     a
                       1                            b
                                                     c
_____________________________
                                                      x
                        2                            y
                                                       z

        代        码:
                
        /// <summary>
        
/// used to merge cells
        
/// </summary>
        
/// <param name="dg">datagrid will be merged</param>
        
/// <param name="GroupColumn">merged column</param>
        
/// <param name="compareColumn">basis column</param>

        private void spanRow(DataGrid dg,int GroupColumn,int compareColumn)
        
{
            
int i = 0;
            
int j = 0;
            
int rowSpan;
            
string strTemp = "";
 
            
for(i=0;i<dg.Items.Count;i++)
            
{
                rowSpan 
= 1;
                strTemp 
= dg.Items[i].Cells[compareColumn].Text;
                
for (j=i+1;j<dg.Items.Count;j++)
                
{
                    
if (string.Compare(strTemp,dg.Items[j].Cells[compareColumn].Text) == 0)
                    
{
                        rowSpan 
+= 1;
                        dg.Items[i].Cells[GroupColumn].RowSpan 
= rowSpan;
                        dg.Items[j].Cells[GroupColumn].Visible 
= false;
                    }

                    
else
                    
{
                        
break;
                    }

                }

                i 
= j -1;
            }

        }

        以上两种方法可合并使用