1

我创建了一个带有浮动标题的网格视图,当我滚动时该标题保持原位,但是网格视图是动态生成的,标题和列会自动适应内容。因为标题与内容分开浮动,所以它的宽度与 gridview 中的列不同,并且因为它是动态生成的,所以我不想分配预定义的宽度。我正在尝试通过c#获取gridview第一行中每一列的宽度,并设置每列的标题宽度以匹配该宽度。现在我正在尝试:

<asp:GridView CssClass="Grid" ID="gv" runat="server" OnRowDataBound="gridViewDataBind">

用 c#

    protected void gridViewDataBind(object sender, GridViewRowEventArgs e)
{
    for (int c = 0; c < e.Row.Cells.Count; c++)
    {
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            int maxWidth = 0;
            //iterate through each cell in the row
            //this loop will search for the widest cell
            if (e.Row.Cells[i].Text.Length > maxWidth)
            {
                maxWidth = e.Row.Cells[i].Text.Length;
                //update the header cell to match the maxWidth found
                //I multiplied by 10 to give it some length
                Unit u_maxWidth = Unit.Parse((maxWidth * 16).ToString());
                gv.HeaderRow.Cells[i].Width = u_maxWidth;
                e.Row.Cells[i].Width = u_maxWidth;
            }
            if ((gv.HeaderRow.Cells[i].Text.Length) > maxWidth)
            {
                maxWidth = gv.HeaderRow.Cells[i].Text.Length;
                //update the header cell to match the maxWidth found
                //I multiplied by 10 to give it some length
                Unit u_maxWidth = Unit.Parse((maxWidth * 16).ToString());
                gv.HeaderRow.Cells[i].Width = u_maxWidth;
                gv.Columns[i].ItemStyle.Width = u_maxWidth;
            }
        }
    }
}

编辑:这是现在设置宽度,但是设置单元格宽度并没有给我我正在寻找的结果。当我尝试设置列宽时,我收到一个错误,即它大于集合,因为没有其他列具有宽度。

4

2 回答 2

0

编辑 由于我们无法获得单位宽度,让我们强制它。我们知道数据正在进入并且具有该数据的长度。根据您使用的字体系列,您可以这样操作它。我使用了来自 2 个对象的样本数据。

将 RowDataBound 事件之外的 int maxWidth 移到类范围内,否则每次填充一行时它都会重置。

private int maxWidth = 0;

更新 DataRow 条件语句,如下所示:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    log.Debug("Found a datarow");

    for (int i = 0; i < e.Row.Cells.Count; i++)
    {
        log.Debug(String.Format("Row.Cell length : {0} || maxWidth : {1}", e.Row.Cells[i].Text.Length, maxWidth));

        //iterate through each cell in the row
        //this loop will search for the widest cell
        if (e.Row.Cells[i].Text.Length > maxWidth)
        {
            maxWidth = e.Row.Cells[i].Text.Length;
            log.Debug(String.Format("maxWidth is now : {0}", maxWidth));

            //update the header cell to match the maxWidth found
            //I multiplied by 10 to give it some length
            Unit u_maxWidth = Unit.Parse((maxWidth * fontFamilyFactor).ToString());

            log.Debug(String.Format("u_maxWidth is now : {0}", u_maxWidth));
            gv.HeaderRow.Cells[i].Width = u_maxWidth;
        }
    }
}

我使用 log4net 作为我的调试器。这是我的日志文件的输出,其中包含我运行的示例数据:

Found a datarow
Row.Cell length : 0 || maxWidth : 0
Row.Cell length : 7 || maxWidth : 0
maxWidth is now : 7
u_maxWidth is now : 70px
Row.Cell length : 13 || maxWidth : 7
maxWidth is now : 13
u_maxWidth is now : 130px
Row.Cell length : 0 || maxWidth : 13
Row.Cell length : 12 || maxWidth : 13
Row.Cell length : 0 || maxWidth : 13

Found a datarow
Row.Cell length : 0 || maxWidth : 13
Row.Cell length : 3 || maxWidth : 13
Row.Cell length : 13 || maxWidth : 13
Row.Cell length : 0 || maxWidth : 13
Row.Cell length : 12 || maxWidth : 13
Row.Cell length : 0 || maxWidth : 13
Row.Cell length : 0 || maxWidth : 13
于 2013-07-02T14:09:05.093 回答
0

我发现的问题是,因为必须启用自动生成列,所以生成的列“宽度”为空并自动适应单元格的内容。我无法填写宽度,因为它会比在渲染之前没有宽度的表格更宽。由于在渲染表格后我无法自动调用方法,因此我无法使用该方法来应用宽度。

最终,我使用文字控件生成与 HTML 表格相同的表格,并根据单元格中的字符数分配宽度。

于 2013-07-06T21:02:04.430 回答