在ASP.NET中以编程方式设置gridview列宽 [英] Set gridview column width programmatically in asp.net
问题描述
需要以编程方式在asp.net中设置gridview的列宽。
**自动生成的列(即AutogenerateColumns = true)。
Need to set the column width of a gridview in asp.net programmatically. ** Autogenerated Columns (i.e., AutogenerateColumns = "true").
我尝试了以下操作;
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[2].Width = Unit.Pixel(200);
}
但没有用。
推荐答案
这是我在ASPX文件上的 GridView1
This is my GridView1
on aspx file
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
Font-Size="Small" Width="800px" OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:CommandField SelectText="Seç" ShowSelectButton="True"/>
</Columns>
</asp:GridView>
这是我在代码隐藏区中以编程方式设置GridView列宽的地方,实际上是关于设置单元格的宽度但是它控制列的宽度,所以这是一种方法。您可以看到我没有 AutogenicColumns = True
,尽管我认为这并不重要,因为 GridView.RowDataBound
当数据行绑定到数据时发生。
This is where I set my GridView's column width programmatically in codebehind.It is actually about setting the cell's width but it controls the column width so this is a way.As you can see I do not have AutogeneratedColumns="True"
, though I do not think that would matter because GridView.RowDataBound
occurs when a data row is bound to data.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[1].Width = 1;
e.Row.Cells[0].Width = 1;
e.Row.Cells[4].Width = 75;
e.Row.Cells[5].Width = 1;
}
这篇关于在ASP.NET中以编程方式设置gridview列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!