css - 如何设置Gridview的列宽

标签 css gridview

下面是我的两个 GridView 。

enter image description here

下面是两个gridviews的代码。

第一个 GridView :

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
    BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
    EnableModelValidation="True" GridLines="Horizontal" CssClass="mGrid" ShowFooter="True"
    OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="SFTI_Name" HeaderText="SFTI">
            <ItemStyle Width="80px" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Sanctioned  Budget">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblSanctionedDPR" Text='<%#CheckNull(String.Format("{0:N}", Eval("Sanctioned_DPR")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblSanctioned_DPR" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Contract Budget">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblSanctionedContract" Text='<%#CheckNull(String.Format("{0:N}", Eval("Sanctioned_Contract")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblSanctioned_Contract" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCEligibleExp" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPEligibleExp" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
</asp:GridView>

第二个 GridView :

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White"
    BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
    EnableModelValidation="True" GridLines="Horizontal" CssClass="mGrid" ShowFooter="True"
    OnRowDataBound="GridView2_RowDataBound">
    <Columns>
        <asp:BoundField DataField="SFTI_Name" HeaderText="SFTI">
            <ItemStyle Width="80px" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Sanctioned  Budget">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblSanctionedDPR" Text='<%#CheckNull(String.Format("{0:N}", Eval("Sanctioned_DPR")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblSanctioned_DPR" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Contract Budget">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblSanctionedContract" Text='<%#CheckNull(String.Format("{0:N}", Eval("Sanctioned_Contract")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblSanctioned_Contract" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCEligibleExp" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("C_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblC_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPEligibleExp" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblPTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("P_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblP_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Non-Eligible">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPNonEligible_Exp" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Non_Eligible_Exp")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Non_Eligible_Exp" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Total">
            <ItemTemplate>
                <asp:Label runat="server" ID="lblCPTotal" Text='<%#CheckNull(String.Format("{0:N}", Eval("CP_Total")))  %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblCP_Total" DataFormatString="{0:N}" runat="server" Font-Bold="true"></asp:Label>
            </FooterTemplate>
            <FooterStyle HorizontalAlign="Right" />
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Right" VerticalAlign="Top" Wrap="False" Width="80px" />
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="White" ForeColor="#333333" />
    <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
    <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
</asp:GridView>

我已将每个列的宽度设置为 80px。但是你可以看到两者的宽度不同。我喜欢格式化那些第二个 gridview 的所有 12 列应该与第一个 gridview 位于相同的列。因为第一个 gridview 的数量很大,所以列会扩展,但是第二个 gridview 的数量较少,所以它不会相应地扩展到第一个。

我该怎么做。

最佳答案

这可以帮助你,即使我遇到了 GridView 超出屏幕的同样问题。

http://www.codeproject.com/Tips/385449/Docking-a-GridView-and-add-a-horizontal-scrollbar

<asp:Panel ID="panel02" ScrollBars="Horizontal" runat="server" Width="100%" Visible ="false">

关于css - 如何设置Gridview的列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20881335/

相关文章:

javascript - 对齐 Google 可视化饼图,使图表位于页面中央

asp.net - 如何使用 jQuery 设置表格单元格的背景颜色

Android Gridview 按后退按钮时不刷新

sqlite - 如何从下到上检索 SQLite 数据库表值(最后一行到第一行)

css - 如何删除分配的 Bootstrap 关键字

python - 我的网络抓取代码目前有效,我想更具体一点,我可以通过告诉它上面的特定 header 来选择要抓取的数据吗?

html - 使用绝对响应在图像上定位图标

javascript - 是否存在 XML 对象遵循的高度或宽度的 DOM 或 CSS 属性

带有扩展面板行的 ASP.net 2.0 Gridview -- 如何构建面板 "on the fly"

asp.net-mvc - 可编辑的 "GridView"像 ASP.NET MVC 中的控件