无论如何修复 ASP.NET gridview 中的列宽

Fixing the column width in ASP.NET gridview no matter what

我有一个 ASP.NET 带有方形单元格的 GridView(GridView 的宽度和高度相同,列数和行数也相同/没有 headers)。每个单元格都包含一个 LinkBut​​ton(有时带有一个小的 Font Awesome 图标)。

当我加载 GridView 时,列宽发生变化。如何确保列宽保持固定,即使文本(或小图像)比单元格大?

我在 google 上查找它没有成功。不知何故,我尝试的一切最终都会出现在 "inflating" 专栏中。它只需要强制宽度保持不变。

尝试使用TemplateField,直接将其ItemStyle宽度设置为你想要的固定值

<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="False">
  <Columns>
    <asp:TemplateField HeaderText="" ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Center">
      <%--<ItemStyle Width="80px" />--%><!--OR use this format-->
      <ItemTemplate>
        <asp:Button ID="HiddenButton" runat="server" CommandName="CellClick" Visible="false"/>                                                                             
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>