2010年6月11日 星期五

[C# ASP.NET] Web DataTABLE to TABLE

  public Table CreateTableByDatatable(DataTable ds)
        {
            Table tb = new Table();  //主表
            TableRow tr = null;      //列
            TableCell tc = null;     //行
            Label lb = null;
            HyperLink lk = null;

            //=========================================================設定 Table 參數
            tb = new Table();
            //tb.Width = 1000;
            tb.Font.Name = "Courier New";
            tb.CellPadding = 1;
            tb.CellSpacing = 1;
            tb.BorderStyle = BorderStyle.Solid;
            tb.BorderWidth = 0;
            tb.BorderColor = System.Drawing.Color.FromName("#C0C0C0");
            //tb.GridLines = GridLines.Both;
            //=========================================================設定 標題
            tr = new TableRow();
            lb = new Label();
            tb.Rows.Add(tr);
            tc = new TableCell();
            tc.Attributes.Add("Style", "border-width:1px;border-color:#006699;border-style:solid");
            tc.ColumnSpan = ds.Columns.Count + 1;          //合併儲存格
            tc.HorizontalAlign = HorizontalAlign.Center;
            //lb.Width = ds.Columns.Count  * 70 + 140;
            lb.Text = "Raw Data";
            lb.BackColor = System.Drawing.Color.FromName("#17375D");
            lb.ForeColor = System.Drawing.Color.FromName("#FFFFFF");
            tc.BackColor = lb.BackColor;
            tc.Controls.Add(lb);
            tr.Cells.Add(tc);
            //=========================================================
            tr = new TableRow();
            for (int i = 0; i < ds.Columns.Count; i++)
            {
                lb = new Label();
                tb.Rows.Add(tr);
                tc = new TableCell();
                tc.Attributes.Add("Style", "border-width:1px;border-color:#006699;border-style:solid");
                tc.HorizontalAlign = HorizontalAlign.Center;
                //lb.Width = ds.Columns.Count * 70 + 140;
                lb.Text = ds.Columns[i].ColumnName;
                lb.BackColor = System.Drawing.Color.FromName("#F5FF8E");
                lb.ForeColor = System.Drawing.Color.FromName("#000000");
                tc.BackColor = lb.BackColor;
                tc.Controls.Add(lb);
                tr.Cells.Add(tc);
            }
            //========================================================
            for (int j = 0; j < ds.Rows.Count; j++)
            {
                if (cmbFunction.SelectedValue != "SELECT")
                {
                    if (cmbFunction.SelectedValue == ds.Rows[j].ItemArray[3].ToString())
                    {
                        tr = new TableRow();
                        tb.Rows.Add(tr);
                        for (int i = 0; i < ds.Columns.Count; i++)
                        {
                            lb = new Label();
                            tc = new TableCell();
                            tc.Attributes.Add("Style", "border-width:1px;border-color:#006699;border-style:solid");
                            tc.HorizontalAlign = HorizontalAlign.Center;
                            //lb.Width = ds.Columns.Count * 70 + 140;
                            lb.Text = ds.Rows[j].ItemArray[i].ToString();
                            lb.BackColor = System.Drawing.Color.FromName("#FFFFFF");
                            lb.ForeColor = System.Drawing.Color.FromName("#000000");
                            tc.BackColor = lb.BackColor;
                            tc.Controls.Add(lb);
                            tr.Cells.Add(tc);

                        }

                    }
                }
                else
                {
                    tr = new TableRow();
                    tb.Rows.Add(tr);
                    for (int i = 0; i < ds.Columns.Count; i++)
                    {
                        if (ds.Columns[i].ColumnName == "FAIL" & int.Parse(ds.Rows[j].ItemArray[7].ToString ())>0)
                        {
                            lk = new HyperLink();
                            tc = new TableCell();
                            tc.Attributes.Add("Style", "border-width:1px;border-color:#006699;border-style:solid");
                            tc.HorizontalAlign = HorizontalAlign.Center;
                            lk.Text = ds.Rows[j].ItemArray[i].ToString();
                            lk.NavigateUrl = "DefectDetail.aspx?date=" + ds.Rows[j].ItemArray[4].ToString() + "&" +
                                                               "fc=" + ds.Rows[j].ItemArray[3].ToString() + "&" +
                                                               "model=" + ds.Rows[j].ItemArray[2].ToString();
                            lk.Target = "_blank";
                            lk.BackColor = System.Drawing.Color.FromName("#FFFFFF");
                            lk.ForeColor = System.Drawing.Color.FromName("#0000FF");
                            tc.BackColor = lk.BackColor;
                            tc.Controls.Add(lk);
                            tr.Cells.Add(tc);
                        }
                        else
                        {
                            lb = new Label();
                            tc = new TableCell();
                            tc.Attributes.Add("Style", "border-width:1px;border-color:#006699;border-style:solid");
                            tc.HorizontalAlign = HorizontalAlign.Center;
                            //lb.Width = ds.Columns.Count * 70 + 140;
                            lb.Text = ds.Rows[j].ItemArray[i].ToString();
                            lb.BackColor = System.Drawing.Color.FromName("#FFFFFF");
                            lb.ForeColor = System.Drawing.Color.FromName("#000000");
                            tc.BackColor = lb.BackColor;
                            tc.Controls.Add(lb);
                            tr.Cells.Add(tc);
                        }

                    }

                }
               

            }

            return tb;


        }

沒有留言:

張貼留言