GridView单元格宽度始终为0

时间:2012-11-02 09:23:42

标签: c# asp.net gridview

我有GridView,其中每列的宽度是动态的,并根据DataSet而变化。我需要得到每列的宽度,但列/单元格宽度始终为'0',可能是因为我没有设置width属性。

有解决方法吗?

这是代码:

        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);

0 个答案:

没有答案