phiree

博客园 首页 联系 订阅 管理
根据LoveCherry的竖变横方法改进而来(http://www.cnblogs.com/lovecherry/archive/2005/04/23/143819.html)

        public DataTable MakeData(DataTable dt,int iColumnSize,int[] iColumnID) //iColumnsize:重复列数 iColumnID 需要横向排列的列的序号数组
        {   
            
int ColumnCount=iColumnID.Length;
            
int totalRows=dt.Rows.Count;
            
int itmp=dt.Rows.Count%iColumnSize; //行数
            int iRows; //行数
            if(itmp==0
                iRows
=dt.Rows.Count/iColumnSize; 
            
else 
                iRows
=dt.Rows.Count/iColumnSize+1
            DataTable newdt
=new DataTable(); 
            
for(int i=0;i<iColumnSize;i++
            
{  
                
for(int j=0;j<ColumnCount;j++)
                
{  string aa=dt.Columns[iColumnID[j]].ColumnName+i;
                    
int bb=iColumnID[j];
                    DataColumn dc
=new DataColumn(dt.Columns[iColumnID[j]].ColumnName+i,dt.Columns[iColumnID[j]].DataType); 
                
                    newdt.Columns.Add(dc); 
                }

            }
 
            
for(int i=0;i<iRows;i++
            
{         
                DataRow dr
=newdt.NewRow(); 
                
for(int j=0;j<iColumnSize;j++
                

                    
for(int k=0;k<ColumnCount;k++)
                    
{
                        
if((i+j*iColumnSize)<dt.Rows.Count) 
                        
{  
                            
string aa=dt.Columns[iColumnID[k]].ColumnName+j;
                            
try
                            
{
                                dr[dt.Columns[iColumnID[k]].ColumnName
+j]=dt.Rows[iRows*j+i][iColumnID[k]]; 
                            }

                            
catch
                            
{}
                        }

                    }

                }
 
                newdt.Rows.Add(dr);                 
            }
 
            
return newdt; 
        }
 
posted on 2005-11-20 00:44  phiree  阅读(2738)  评论(0编辑  收藏  举报