2013-07-02 106 views
1

我创建与保持在适当位置时,我滚动浮动头一个gridview,然而gridview的与报头和列自动拟合的内容动态生成的。由于标头分别从内容浮动其宽度不一样在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; 
      } 
     } 
    } 
} 

编辑:现在这是将宽度,但是设置单元格宽度不给我我想要的结果。当我尝试设置列宽时,由于没有其他列具有宽度,我得到一个比集合大的错误。

回答

0

编辑 由于我们无法获得单位宽度,所以强迫它。我们知道数据正在进入并且有一段数据。取决于正在使用的字体系列,您可以用这种方式操纵它。我使用了2个对象的样本数据。

移动INT maxWidth,那么RowDataBound事件到类范围之内,否则会重置eachtime一行被填充。

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 
+0

谢谢,但我还是有这个问题。 gv vs gv_YourSite是我的一个错字,它们是同一个gridview。在gridview绑定数据后,我会这样做,因此它不应该成为相关文章中的问题。我已经使用新代码更新了原始帖子。再次感谢你的帮助。 –

+0

看起来你对SO文章是正确的。 –

+0

我更新了我的编辑的替代解决方案(黑客) – Ian

0

我发现的问题是,因为自动生成的列有要上,该柱用“宽度”是空的,autofitting以产生细胞的内容。我无法填充宽度,因为它将比直到渲染时没有宽度的表格宽。由于我无法在渲染表格后调用方法,因此我无法使用该方法来应用宽度。

最终我用文字控制产生相同的表作为一个HTML表,并分配根据字符的在细胞中的数量的宽度。