表A
ID CLOS
1  a
2  b
3  c
4  d
5  e
..  ..ds = 表aint l = 0;
int i = Math.DivRem(ds.Tables[0].Rows.Count, 5, out l);
int k = ds.Tables[0].Rows.Count;
for (int j = 0; j < i + 1; j++)
{
//AddRow(ds, j * 5, k);有错
}
要求每行显示5个,多的换行显示。
AddRow(DataSet ds,int i,int j)
{
 //错
}怎么写这个算法呀?

解决方案 »

  1.   

    select top 33 id=identity(int,0,1) into # from sysobjects
    --select * from #
    declare @sql varchar(8000)
    select @sql=isnull(@sql+',','')+'max(case when id%5='''+ltrim(px)+''' then ltrim(id) else '''' end) as ['+ltrim(px)+'列]'
    from (select distinct id%5 as px from #)t
    exec('select '+@sql+' from # group by id/5')0列 1列 2列 3列 4列
    0 1 2 3 4
    5 6 7 8 9
    10 11 12 13 14
    15 16 17 18 19
    20 21 22 23 24
    25 26 27 28 29
    30 31 32
      

  2.   

    强~不过你理解错我的意思了。
    不过我自己解决了。觉得方法很笨。
    贴出来。那位有好方法回贴让我看下。 DataSet ds = data.Sql.Select_ALL();//表A全部数据
                                               if (ds.Tables.Count > 0)
                            {
                                int l = 0;
                                int i = Math.DivRem(ds.Tables[0].Rows.Count, 5, out l);
                                if (l > 0)
                                {
                                    i++;
                                }
                                int k = ds.Tables[0].Rows.Count;
                                for (int j = 0; j < i; j++)
                                {
                                    AddRow(ds, j * 5, k);
                                    
                                }
                            }
    //--
     private void AddRow(DataSet ds,int i,int j)
            {
                TableRow tr = new TableRow();            TableCell tc1 = new TableCell();
                TableCell tc2 = new TableCell();
                TableCell tc3 = new TableCell();
                TableCell tc4 = new TableCell();
                TableCell tc5 = new TableCell();            if (i < j)
                {
                    tc1.Text = "<a /></a>";
                    tc1.CssClass = "tc1";
                }
                if (++i < j)
                {
                    tc2.Text = "<a /></a>";
                    tc2.CssClass = "tc2";
                }
                if (++i < j)
                {
                    tc3.Text = "<a /></a>";
                    tc3.CssClass = "tc3";
                }
                if (++i < j)
                {
                    tc4.Text = "<a /></a>";
                    tc4.CssClass = "tc4";
                }
                if (++i < j)
                {
                    tc5.Text = "<a /></a>";
                    tc5.CssClass = "tc5";
                }
                     tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                tr.Cells.Add(tc3);
                tr.Cells.Add(tc4);
                tr.Cells.Add(tc5);            Table1.Rows.Add(tr);
            }