0

我有以下代码:

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
        }
    }

是否也可以设置单元格的高度?谢谢!

4

1 回答 1

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;
}

这个对我有用。

于 2013-03-27T06:40:19.167 回答