2015-06-08 42 views
0

如何在Dynamic Gridview中设置列宽?当的AutoGenerateColumns = “真”如何在动态网格视图中设置列宽?

+0

添加一些代码..? –

+1

@Ameer:你有没有尝试过[某事从这里](http://www.dotnetgallery.com/kb/resource69-Set-Gridview-column-width-dynamically-using-C.aspx) – BNN

+0

感谢Nadeem它的工作:) –

回答

0

你需要做这样的

protected void gvData_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 
    if (e.Row.RowType == DataControlRowType.DataRow) { 
     e.Row.Cells(0).Width = new Unit("200px"); 
     e.Row.Cells(1).Width = new Unit("500px"); 
    } 
    } 

您的标记

<asp:GridView id="gvData" runat="server"     
       OnRowDataBound="gvData_RowDataBound"> 
</asp:GridView> 

默认情况下autogeneratecolumn变化的GridView的RowDataBound事件是假的所以没有必要指定autogeneratecolumn = “true”

+0

上面这两个解决方案不工作在我的情况:(默认情况下 –

0

您可以有如下serverside方法:

private void GV_RowDataBound(object o, GridViewRowEventArgs e) 
{   
    // apply custom formatting to data cells 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // set formatting for the category cell 
     TableCell cell = e.Row.Cells[0]; 
     cell.Width = new Unit("120px"); 
     cell.Style["border-right"] = "2px solid #666666"; 

     // set formatting for value cells 
     for(int i=1; i<e.Row.Cells.Count; i++) 
     { 
      cell = e.Row.Cells[i]; 
      // right-align each of the column cells after the first 
      // and set the width 
      cell.HorizontalAlign = HorizontalAlign.Right; 
      cell.Width = new Unit("90px"); 
      // alternate background colors 
      if (i % 2 == 1) 
        cell.BackColor 
         = System.Drawing.ColorTranslator.FromHtml("#EFEFEF"); 
        // check value columns for a high enough value 
        // (value >= 8000) and apply special highlighting 
      }      
     } 

     // apply custom formatting to the header cells 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      foreach (TableCell cell in e.Row.Cells) 
      { 
       cell.Style["border-bottom"] = "2px solid #666666"; 
       cell.BackColor=System.Drawing.Color.LightGray; 
      } 
     } 

    } 
} 

aspx

<asp:GridView id="myList" runat="server" 
        AutoGenerateColumns="true" 
        OnRowDataBound="GV_RowDataBound" 
        . . . 
        > 
</asp:GridView> 

有关详细信息,您可以检查here

+0

AutoGenerateColumns是“true”。所以没有提及 – Alex

+0

我同意,但我认为你需要检查你的答案,因为你已经提到_By默认autogeneratecolumn是** false **所以不需要指定autogeneratecolumn =“true”_ :) –

相关问题