C# 根据列宽动态设置标题宽度
我创建了一个带有浮动标题的gridview,该标题在我滚动时保持不变,但是gridview是动态生成的,标题和列自动匹配内容。由于标题与内容分开浮动,其宽度与gridview中的列不同,并且是动态生成的,因此我不想指定预定义的宽度。我试图通过c获得gridview第一行中每一列的宽度,并将每一列的标题宽度设置为与之匹配。现在我正在尝试:C# 根据列宽动态设置标题宽度,c#,asp.net,gridview,C#,Asp.net,Gridview,我创建了一个带有浮动标题的gridview,该标题在我滚动时保持不变,但是gridview是动态生成的,标题和列自动匹配内容。由于标题与内容分开浮动,其宽度与gridview中的列不同,并且是动态生成的,因此我不想指定预定义的宽度。我试图通过c获得gridview第一行中每一列的宽度,并将每一列的标题宽度设置为与之匹配。现在我正在尝试: <asp:GridView CssClass="Grid" ID="gv" runat="server" OnRowDataBound="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;
}
}
}
}
编辑:这是现在设置的宽度,但设置单元格宽度并没有给我的结果,我正在寻找。当我试图设置列宽时,我得到一个错误,即它大于集合,因为没有其他列具有宽度。EDIT
既然我们不能得到单位宽度,让我们强制它。我们知道数据正在进入,并且有一个数据长度。根据所使用的字体系列,您可以这样操作它。我使用了来自两个对象的样本数据
将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;
}
}
}
我发现的问题是,因为必须启用“自动生成列”,所以生成列时,列的宽度为空,并且自动适应单元格的内容。我无法填充宽度,因为它将比在渲染之前没有宽度的表宽。由于我无法在呈现表后自动调用方法,因此我无法使用该方法应用宽度
最后,我使用文字控件生成与HTML表相同的表,并根据单元格中的字符数分配宽度。谢谢,但我仍然存在这个问题。gv vs gv_YourSite是我的错别字,它们是相同的gridview。我在gridview被数据绑定并呈现之后才这样做,因此它不应该是相关文章中看到的问题。我已经用新代码更新了原来的帖子。再次感谢您的帮助。很抱歉,我对c没有经验,也没有配置调试器,但是通过新的设置和观察它的调试,单元格宽度现在已经设置好了。但是,设置单元格长度会产生不一致的结果。我想我想改为设置gv_YourSite.Columns[I].ItemStyle.Width,但是当我尝试时,收到的错误索引超出了范围。必须为非负数且小于集合的大小。我现在尝试将列宽设置为新宽度不大于集合的大小,因为它们将使用该方法重新调整大小。谢谢你的帮助。
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