DataTable数据集动态构造Table表结构

功能介绍:

根据入库单编号和产品编码,获取该入库单中该产品的颜色尺码值,以二维形式显示出来。即把DataTable数据集

 

 

(图1用以下格式显示出来 (图2

实现代码:

前台:

现在页面中拉一个table控件:

<asp:Table ID="Table1" runat="server" CssClass="grid" Width="92%" CellPadding="2" CellSpacing="0" style="text-align:center;" GridLines="Both">

</asp:Table>

后台:

DataTable dt = new DataTable();

DataTable colors = new DataTable();

DataTable sizes = new DataTable();

dt = SQLServerDAL.Report.SqlReportMgt.GetColorSizeByCC(billcode, barcode);//获得图1的数据集

colors = SQLServerDAL.Report.SqlReportMgt.GetColors(billcode, barcode);//获取该订单该产品的颜色集(即【粉红,黑】)

sizes=SQLServerDAL.Report.SqlReportMgt.GetSizes();//获取所有尺码(即【L,M,S,XL】)

 

TableRow tr=new TableRow();//

                TableCell tc=new TableCell();//

                Label lb;

                //第一行标题

                tc.Text = "入库单:" + billcode + " 中产品" + barcode+ " 的信息";

                tc.Attributes.Add("colspan", "6");

                tc.Attributes.Add("text-align", "center");

                tc.Attributes.Add("background", "RGB(192,192,192)");

                tr.Cells.Add(tc);               

                Table1.Rows.Add(tr);

                //第一列颜色列名

                tr = new TableRow();

                tr.HorizontalAlign = HorizontalAlign.Center;

                            

                tc = new TableCell();

                tc.Text = "?¦?";

                tr.Cells.Add(tc);

                //生成表头

                for (int i = 0; i <= sizes.Rows.Count; i++)

                {

                    tc = new TableCell();

                    if (i < sizes.Rows.Count)

                    {

                        tc.Text = sizes.Rows[i][0].ToString();

                    }

                    else

                    {

                        tc.Text = "合计";

                    }                  

                    tr.Cells.Add(tc);

                }

                Table1.Rows.Add(tr);           

                for (int i = 0; i < colors.Rows.Count; i++)//颜色

                {

                    tr = new TableRow();

                    tc = new TableCell();

                    lb = new Label();//记录第一列的颜色值

                    lb.Text = colors.Rows[i][0].ToString();

                    tc.Controls.Add(lb);

                    tr.Cells.Add(tc);

                    int total = 0;//记录合计

                    for (int j = 0; j < sizes.Rows.Count; j++)//尺码

                    {

                        tc = new TableCell();

                        lb = new Label();

                        //给每一个单元格附一个label控件

                        lb.ID ="lbl_"+ colors.Rows[i][0].ToString() + sizes.Rows[j][0].ToString();//形如粉红S”“粉红M”

                        for (int t = 0; t < dt.Rows.Count; t++)//遍历数据集

                        {

                            if (dt.Rows[t][2].ToString() == colors.Rows[i][0].ToString() &&

                                dt.Rows[t][3].ToString() == sizes.Rows[j][0].ToString())

                            {

                                lb.Text = dt.Rows[t][4].ToString();

                                total = total + Convert.ToInt32(lb.Text);//合计累加

                            }

                        }

                        tc.Controls.Add(lb);

                        tc.Attributes.Add("text-align", "center");

                        tr.Cells.Add(tc);

                    }

                    lb = new Label();//定义合计列

                    tc = new TableCell();

                    lb.ID = "lbl_" + colors.Rows[i][0].ToString() + "合计";

                    lb.Text = total.ToString();

                    tc.Controls.Add(lb);

                    tr.Cells.Add(tc);

                    Table1.Rows.Add(tr);

                }

 

说明:

1. 生成时只能一行一行的生成

2. 生成行列时,先给行、列分别一个实例化变量,即tr = new TableRow();                    tc = new TableCell();,再实例化一个label控件:lb = new Label();设置label的文本,然后把label加入到列tc中:tc.Controls.Add(lb);,然后把列加入到行中:tr.Cells.Add(tc);,最后把行加入到table中:Table1.Rows.Add(tr);

3. 反复第2步,定义每一行内容。

 

 

posted @ 2011-08-01 18:34  Alec-Yin  阅读(4505)  评论(1编辑  收藏  举报
更多前端博客,访问:http://lhyin.com