我有以下代码:
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 :(得分:1)
您在RowDataBound()事件中设置宽度。你不能这样做,在databind()发生之前尝试columns属性。
实施例
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;
}
它对我有用。