gpt4 book ai didi

asp.net - GridView - 在后面的代码中设置列宽 (AutoGenerateColumns ="true")

转载 作者:行者123 更新时间:2023-12-05 02:21:42 26 4
gpt4 key购买 nike

我的 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 填充成功。
请注意,我可以设置列的对齐方式和背景色,但不能设置其宽度。
我尝试了很多解决方案,但没有一个奏效。
它总是将列的大小调整为最长的内容。
完全可以做到吗...?

最佳答案

我可以使用 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");

}

关于asp.net - GridView - 在后面的代码中设置列宽 (AutoGenerateColumns ="true"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32702276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com