I'm using
<asp:GridView ID="Grid1" runat="server" AutoGenerateColumns="False" BorderWidth="0px" BorderColor="white" BorderStyle="Solid">
<asp:TemplateField HeaderText="tf1" ControlStyle-Width="40px">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# String.Format("{0:###,###,###,##0}",Convert.ToInt64(DataBinder.Eval(Container.DataItem,"tf1"))) %>' />
<asp:TextBox id="txt1" runat="server" Text='<%# Eval("tf1") %>' style="display:none;text-align:right;" MaxLength = "9" />
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
The problem is eventhough the ConrolStyle-Width is set to 40 px, I want it to be dynamic in size to accomodate data which requires more than 40px space. How can I achieve this?
Instead of setting ControlStyle-Width="40px" just set css class like this:
ControlStyle-CssClass="template"
and then style your fields as you want. You can add min-width which means all fields will have minimum width equal to specified and if content width is larger than field width will fit it content.
.template{
min-width: 40px;
}
来源:https://stackoverflow.com/questions/23423075/setting-the-width-of-the-grid-view-column-to-dynamic-without-compromising-the-mi