0

我有一个GridView,其中每列的宽度是动态的,并根据DataSet. 我需要获取每列的宽度,但列/单元格宽度始终为“0”,大概是因为我没有设置宽度属性。

有没有解决的办法?

这是代码:

        gridResult.DataSource = dtResult;
    gridResult.DataBind();

    for (int i = 0; i < gridResult.Rows.Count; i++)
    {
        for (int j = 0; j < gridResult.Rows[i].Cells.Count; j++)
        {
            string encoded = gridResult.Rows[i].Cells[j].Text;
            gridResult.Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);

        }
    }

    GridView2.ShowHeaderWhenEmpty = true;

    int tblCols = gridResult.HeaderRow.Cells.Count;

    Table tbl = new Table();
    TableRow tr = new TableRow();
    double check = gridResult.Columns[0].ItemStyle.Width.Value;
    double check2 = gridResult.Rows[0].Cells[0].Width.Value;
       // TableRow tr = new TableRow();
        for (int j = 0; j < tblCols; j++)
        {

            // Add the table to the placeholder control
            TableCell td = new TableCell();
            td.Text = gridResult.HeaderRow.Cells[j].Text;
            td.Attributes.Add("width", gridResult.HeaderRow.Cells[j].Width + "px");
            td.HorizontalAlign = HorizontalAlign.Center;
            tr.Cells.Add(td);
            td.BorderWidth = 1;
            td.BorderColor = System.Drawing.Color.Black;

        }
        tbl.Rows.Add(tr);
    PlaceHolder1.Controls.Add(tbl);
4

0 回答 0