3

私のGridView:

<asp:GridView ID="GridView1" runat="server"
    OnRowDataBound="GridView1_RowDataBound"
    AutoGenerateColumns="true"
    DataKeyNames="Role_id">
</asp:GridView>

コードビハインド:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
       //e.Row.Cells[2].Width = 120;                      // isn't working....
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[2].Width = new Unit(120);             // isn't working....
        TableCell cell = e.Row.Cells[2];
        cell.HorizontalAlign = HorizontalAlign.Right;           //**** does work!
        cell.BackColor = Color.LightGray;                       //**** does work!
        //cell.Width = 120;                               // isn't working....
        //e.Row.Cells[2].Width = new Unit("120px") ;      // isn't working....
        //e.Row.Cells[2].CssClass = "myGV_Cell_Width";    // isn't working....
    }
}

GridView が正常に設定されました。列の配置と背景色は設定できますが、幅は設定でき
ないことに注意してください。 出回っている多くのソリューションを試しましたが、どれもうまくいきませんでした。 列のサイズは常に最長のコンテンツに変更されます。 果たして出来るのか…?


4

2 に答える 2

3

RowCreated イベントでそれを行うことができましたが、グリッドの幅を設定し、グリッドを table-layout:fixed に設定する必要がありました

<asp:GridView ID="GridView1" runat="server"
    style="table-layout:fixed;" Width="1000px"
    OnRowCreated = "GridView1_RowCreated"
    OnRowDataBound="GridView1_RowDataBound"
    AutoGenerateColumns="true"
    DataKeyNames="Role_id">
</asp:GridView>

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{

 e.Row.Cells[0].Width = New Unit("220px");

}
于 2015-09-22T12:13:59.600 に答える