如何设置Gridview列的宽度

时间:2014-01-02 10:55:23

标签: css gridview

见下面是我的两个网格视图。

enter image description here

以下是两个网格视图的代码。

第一个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>

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

我该怎么做。

1 个答案:

答案 0 :(得分:0)

这可以帮到你,即使我遇到同样的问题,网格视图也会出现在屏幕上。

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">