C# – ASP.NET Set GridView Column max width size

asp.netcgridview

I am using ASP.NET C# on VS2005.

I have a GridView table with a column named Description, and since the input is always very long, the description is very long horizontally.

I would want the GridView to have a max width size for all columns.

I have tried many ways but none of them worked.

ItemStyle-Width="50px",

HeaderStyle-BorderWidth="50px",

HeaderStyle-Width="50px",

RowStyle-Width="50px",

Width="50px"

Below is a code snippet of my GridView:

<asp:GridView ID="GridView1" AutoGenerateEditButton="True" ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="UserSelector" OnCheckedChanged="UserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Anyone know how to adjust the size of GridView columns based on my situation?

Best Answer

This is how I control the column width.

1.Add the CSS in the header.

<style type="text/css">
    .maxWidthGrid
    {
        max-width: 300px;
        overflow: hidden;
    }
</style>

2.Then use the CSS in necessary columns like this ItemStyle-CssClass="maxWidthGrid"

<asp:BoundField ItemStyle-CssClass="maxWidthGrid" DataField="ClientName" HeaderText="Client Name"
                    ReadOnly="True" SortExpression="ClientName" />