日期:2010-07-07  浏览次数:20537 次

竖表变横表(支持固定列)

根据LoveCherry的竖变横方法改进而来
        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;