友情链接: 互动百科 CSDN.NET 百度音乐 和讯理财 世界杯吧 拉手网

在.NET中,将竖表变横表(支持固定列)

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

根据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;
                            try
                            {
                                dr[dt.Columns[iColumnID[k]].ColumnName+j]=dt.Rows[iRows*j+i][iColumnID[k]];
                            }
                            catch
                            {}
                        }
                    }
                }
                newdt.Rows.Add(dr);                
            }
            return newdt;
        }

posted on 2006-03-16 23:48  行万里路 责任 创新 执着  阅读(640)  评论(1编辑  收藏  举报