2013-03-27 28 views
0

我有以下代碼的高度:設定的行,方法的RowDataBound

protected void exampleGridView_RowDataBound(object o, GridViewRowEventArgs e) 


    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Cells[0].Width = new Unit("150px"); 
      e.Row.Cells[1].Width = new Unit("5px"); 
      e.Row.Cells[2].Width = new Unit("150px"); 
      e.Row.Cells[3].Width = new Unit("150px"); 
      e.Row.Cells[4].Width = new Unit("150px"); 
      e.Row.Cells[5].Width = new Unit("150px"); 
      e.Row.Cells[6].Width = new Unit("150px"); 
      // and so on 
     } 
    } 

,纔有可能設置單元格的高度呢? 謝謝!

+0

我認爲它的ASP.NET不是asp-classic標記的, – Habib 2013-03-27 06:04:07

+0

我試着用e.Row.Cells [0] .Height = new Unit(「25px」);但它並沒有真正改變行的高度 – user1781830 2013-03-27 06:09:08

+0

請嘗試:e.Row.Cells [0] .Height = 25; – 2013-03-27 07:30:17

回答

1

您正在設置RowDataBound()事件的寬度。你不能這樣做,在databind()發生之前嘗試列屬性。

GridView1.CellPadding = 20; 
GridView1.RowStyle.Height = 80; 

或 你可以試試這個樣本

在ASPX

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false" 
     runat="server"> 
     <Columns> 
      <asp:BoundField DataField="CatID" HeaderText="ID" /> 
      <asp:BoundField DataField="CatName" HeaderText="Name" /> 
     </Columns> 
    </asp:GridView> 
中的.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    List<Category> _lstCategory = new List<Category>{new Category { CatID = 1, CatName = "Cat1" }, 
                 new Category { CatID=2,CatName="Cat2" }}; 
    //GridView1.CellPadding = 20; 
    //GridView1.RowStyle.Height = 80; 
    GridView1.DataSource = _lstCategory; 
    GridView1.DataBind(); 

} 
public class Category 
{ 
    public int CatID { get; set; } 
    public string CatName { get; set; } 
} 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[1].Width = 100; 
    e.Row.Cells[0].Width = 1; 
} 

它爲我工作。

+0

對不起,我是新來的asp,我應該怎麼做? – user1781830 2013-03-27 06:54:07

+0

@ user1781830我剛剛使用代碼更新了我之前的回覆。請檢查。 – 2013-03-27 10:25:52