[英]Unable to set GridView row widths on RowDataBound
My current code to attempt to set widths for the rows on a gridview is: 我当前的尝试为gridview上的行设置宽度的代码是:
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (DataControlFieldHeaderCell dcfhc in e.Row.Cells)
{
dcfhc.Width = 100;
}
}
else
{
foreach (DataControlFieldCell dcfc in e.Row.Cells)
{
dcfc.Width = 100;
}
}
}
But that just keep all of the widths default! 但这只是保持所有宽度为默认值! What am I doing wrong?
我究竟做错了什么?
EDIT: I have already tried thise code as well! 编辑:我已经尝试过此代码!
PoolToDBHeaders.DataSource = new DataView(headerTable);
PoolToDBHeaders.DataBind();
foreach (DataControlField dcf in PoolToDBHeaders.Columns)
{
dcf.ItemStyle.Width = 100;
}
PoolToDBHeaders.DataBind();
...why are you setting the widths in a databinding event? ...为什么要在数据绑定事件中设置宽度? Just set them (using the
Columns
property of the DataGrid) before databinding in the first place. 只需在进行数据绑定之前设置它们(使用DataGrid的
Columns
属性)。
Setting them in a databinding event is silly anyways, since you can't possibly have different widths for different rows. 无论如何,将它们设置为数据绑定事件是很愚蠢的,因为不同行的宽度不可能相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.