Set gridview column width programmatically in asp.net

喜你入骨 提交于 2019-12-10 11:27:41

问题


Need to set the column width of a gridview in asp.net programmatically. ** Autogenerated Columns (i.e., AutogenerateColumns = "true").

I tried the following;

protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[2].Width = Unit.Pixel(200);
}

but no use.


回答1:


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>

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;                
}


来源:https://stackoverflow.com/questions/10980365/set-gridview-column-width-programmatically-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!