Fixed header column width in gridview?

I want to set the width of the header column to represent the grid. I have tried HeaderStyle-Width="30px". But it depends on the columns of Item. The column width is set based on the value of the element. But I want a fixed width, even if it matters or is empty.

alt text

+3
source share
2 answers

You can also specify the title style and width of the ItemStyle. You can also add a range and set the width. Thus, the width of the element will remain the value, whether it is empty or contains any value. I think that small changes to gridview will allow you to achieve a solution to your problem.

<asp:TemplateField HeaderText="Category"  HeaderStyle-HorizontalAlign="Center"
                                    ItemStyle-HorizontalAlign="Center">
     <HeaderTemplate>
                                        <asp:LinkButton ID="lnkCategory" runat="server" ToolTip="Click here to sort by Category"
                                            CommandName="Sort" CommandArgument="Category" Text="Category" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                       <span style="width:50px"> <%#Eval("Category")%> </span>
                                    </ItemTemplate>
                                    <HeaderStyle HorizontalAlign="Left" Width="50px" ></HeaderStyle>
                                    <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
                                </asp:TemplateField>

Impossible changes

<ItemTemplate>
     <span style="width:50px"> <%#Eval("Category")%> </span>
   </ItemTemplate>
  <HeaderStyle HorizontalAlign="Left" Width="50px" ></HeaderStyle>
   <ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
+3

, HeaderStyle-Width = "100px" css , tat u dnt ..

<asp:GridView ID="grdvwnsaids" runat="server" AutoGenerateColumns="False"
                            Width="80%">
                            <Columns>
                                <asp:BoundField HeaderText="Product Name" HeaderStyle-Width="120px" ControlStyle-Font-Bold="true"
                                    ControlStyle-CssClass="MyText" `enter code here`DataField="BrandName">
                                    <ControlStyle CssClass="MyText" Font-Bold="True"></ControlStyle>
                                </asp:BoundField>
                                <asp:BoundField HeaderText="Type" ControlStyle-Font-Bold="true" HeaderStyle-Width="80px"
                                    ControlStyle-CssClass="MyText" DataField="Type">
                                    <ControlStyle CssClass="MyText" Font-Bold="True"></ControlStyle>
                                </asp:BoundField>
                                <asp:BoundField HeaderText="Composition" ControlStyle-Width="280px" ControlStyle-Font-Bold="true"
                                    ControlStyle-CssClass="MyText" DataField="Compositions">
                                    <ControlStyle CssClass="MyText" Font-Bold="True"></ControlStyle>
                                </asp:BoundField>
                                <asp:BoundField HeaderText="Packing" HeaderStyle-Width="100px" ControlStyle-Font-Bold="true"
                                    ControlStyle-CssClass="MyText" DataField="Packings">
                                    <ControlStyle CssClass="MyText" Font-Bold="True"></ControlStyle>
                                </asp:BoundField>
                            </Columns>
                        </asp:GridView>
+1

Source: https://habr.com/ru/post/1775109/


All Articles