首页 > 编程 > .NET > 正文

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

2024-07-10 13:11:32
字体:
来源:转载
供稿:网友

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

根据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;
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表