DataTable两列转换四列

 

ContractedBlock.gif ExpandedBlockStart.gif Code
有时候为了节省打印纸张,通常要吧结果分栏显示,这就需要把DataTable两列转换为四列

报表分页时 添加组并添加表达式:

=Int((RowNumber(Nothing)-1)/15)//每页显示15条记录

(两外一个表达式意义不是很清楚
=Ceiling(RowNumber(Nothing)/20) )

        
/// <summary>
        
/// 两列转换为四列
        
/// </summary>
        
/// <param name="pagesize">每页大小</param>
        
/// <param name="table"></param>
        
/// <returns></returns>
        private DataTable GetStudentRegesterBook(int pagesize,DataTable table)
        {
            
            DataTable dt 
= new DataTable();
            
int rows = table.Rows.Count;
            
if (rows > 0)
            {
                dt.Columns.Add(
"xh1"typeof(string));
                dt.Columns.Add(
"xm1"typeof(string));
                dt.Columns.Add(
"xh2"typeof(string));
                dt.Columns.Add(
"xm2"typeof(string));

                
int pagecount = 0;
                pagecount 
= rows / pagesize;
                
if (pagecount * pagesize < rows)
                    pagecount 
= pagecount + 1;

                
int i, j, k;
                
//考虑前pagecount-1页
                if (rows > 0)
                {
                    
for (i = 0; i < pagecount - 1; i++)
                    {
                        
if (i % 2 == 0)
                        {
                            
for (j = 0; j < pagesize; j++)
                            {
                                DataRow dr 
= dt.NewRow();
                                dr[
"xh1"= table.Rows[j + i * pagesize]["xh"].ToString();
                                dr[
"xm1"= table.Rows[j + i * pagesize]["xm"].ToString();
                                dt.Rows.Add(dr);
                            }
                        }
                        
else
                        {
                            
for (k = 0; k < pagesize; k++)
                            {

                                dt.Rows[k 
+ (i / 2* pagesize]["xh2"= table.Rows[k + i * pagesize]["xh"].ToString();
                                dt.Rows[k 
+ (i / 2* pagesize]["xm2"= table.Rows[k + i * pagesize]["xm"].ToString();

                            }
                        }


                    }
                    
//考虑最后一页
                    
//奇数页
                    if (pagecount % 2 == 1)
                    {

                        
for (k = 0; k < rows - (pagecount - 1* pagesize; k++)
                        {
                            DataRow dr 
= dt.NewRow();
                            dr[
"xh1"= table.Rows[k + i * pagesize]["xh"].ToString();
                            dr[
"xm1"= table.Rows[k + i * pagesize]["xm"].ToString();
                            dt.Rows.Add(dr);
                        }
                    }
                    
else
                    {
                        
//偶数页
                        for (k = 0; k < rows - (pagecount - 1* pagesize; k++)
                        {

                            dt.Rows[k 
+ (i / 2* pagesize]["xh2"= table.Rows[k + i * pagesize]["xh"].ToString();
                            dt.Rows[k 
+ (i / 2* pagesize]["xm2"= table.Rows[k + i * pagesize]["xm"].ToString();

                        }
                    }
                }
            }

                
            
return dt;

        }

转载于:https://www.cnblogs.com/hubcarl/archive/2009/05/10/1453475.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页