根据DataTable 生成 相应的table c#

private void CreateTable(Table tab, DataTable dt, int cellCount)
    {
        if (dt == null)
        {
            return;
        }
        else
        {
            if (dt.Rows.Count < 0)
            {
                tab.Visible = false;
            }
            else
            {
                tab.Visible = true;
                tab.Attributes.Add("width", "95%");
                tab.Attributes.Add("bodercolor", "#999999");
                tab.Style.Add("border-collapse", "collapse");
                tab.Attributes.Add("border", "1");
                tab.Attributes.Add("cellSpacing", "0");
                tab.Attributes.Add("cellPadding", "0");
                tab.BorderStyle = BorderStyle.Solid;
                tab.BorderColor = Color.FromArgb(153, 153, 153);
                //bordercolor=#999999 style=border-collapse:collapse
                int rowCount;
                int recordCount = dt.Columns.Count;
                if (recordCount % cellCount == 0)
                {
                    rowCount = recordCount / cellCount;
                }
                else
                {
                    rowCount = recordCount / cellCount + 1;
                }

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < rowCount; j++)
                    {
                        TableRow r = new TableRow();
                        r.Attributes.Add("height", "10px");
                        r.Attributes.Add("bodercolor", "#999999");
                        r.BorderStyle = BorderStyle.Solid;
                        r.BorderWidth = 1;
                        r.BorderColor = Color.FromArgb(153, 153, 153);
                        r.Attributes.Add("onmouseover", @"this.style.backgroundColor='79c5ea'");
                        r.Attributes.Add("onmouseout", @"this.style.backgroundColor='ffffff'");
                            //添加文本 到新单元格
                        for (int k = 0; k < cellCount;k++)
                        {
                            if (cellCount * j + k < recordCount)
                            {
                                int x = cellCount * j + k;
                                this.NewCell(dt.Columns[cellCount * j + k].ColumnName.ToString().Trim(), r,cellCount);
                                this.NewCell(dt.Rows[i][cellCount * j + k].ToString().Trim(), r,cellCount);
                            }
                            else
                            {
                                this.NewCell("", r, cellCount);
                                this.NewCell("", r, cellCount);
                            }
                         } 
                         tab.Rows.Add(r);
                     }
                     TableRow r1 = new TableRow();
                     r1.Attributes.Add("height", "20px");
                     r1.Attributes.Add("bodercolor", "#999999");
                     r1.BorderStyle = BorderStyle.Solid;
                     r1.BorderWidth = 1;
                     r1.BorderColor = Color.FromArgb(153, 153, 153);
                     r1.Attributes.Add("onmouseover", @"this.style.backgroundColor='ff0000'");
                     r1.Attributes.Add("onmouseout", @"this.style.backgroundColor='ffffff'");
                     tab.Rows.Add(r1);
                }
            }
        }
    }
    private void NewCell(string Text, TableRow r, int cellCount)
    {
        TableCell c = new TableCell();
        c.Style.Add("width", Convert.ToString(100 / (2 * cellCount)) + "%");
        c.BorderColor = Color.FromArgb(153, 153, 153);
        c.BorderStyle = BorderStyle.Solid;
        c.BorderWidth = 1;
        c.Attributes.Add("bodercolor", "#999999");
        c.Style.Add("border-collapse", "collapse");
        c.Attributes.Add("bodercolor", "#ff0000");
        c.Text = Text;
        //c.Attributes.Add("onmouseover", @"this.style.backgroundColor='79c5ea'");
        //c.Attributes.Add("onmouseout", @"this.style.backgroundColor='ffffff'");
        r.Cells.Add(c);
    } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值