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 に答える