【问题标题】:Why is Gridview footer adding an extra column?为什么 Gridview 页脚添加了额外的列?
【发布时间】:2016-03-30 19:37:20
【问题描述】:

不敢相信我不得不问这个问题 - 您会认为这样一个基本功能很容易实现,但我在为 Gridview 创建页脚时遇到了麻烦。我查看了各种教程和其他问题,例如hereherehere,但仍然遇到困难。

问题在于正确显示页脚(即没有添加额外的空列)。根据我收集的信息,您需要将 FooterTemplate 放在 TemplateField 标记中,否则它将不起作用 - 至少它不会为我编译。如果我在 BoundFields 列之后插入它,那么它会添加一个额外的列,这是不可取的。

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
    CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
    OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
    <HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
    <FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
    <Columns>
        <asp:BoundField DataField="FOLDER" HeaderText="Location" />
        <asp:BoundField DataField="FILE" HeaderText="File" />
        <asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
        <asp:BoundField DataField="STATUS" HeaderText="Status" />
        <asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
        <asp:TemplateField>
            <FooterTemplate>
                <asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
            </FooterTemplate>
        </asp:TemplateField>
     </Columns>
</asp:GridView>

同样,如果我将它放在 BoundFields 之前,它会在左侧添加一个额外的列。如果我尝试将所有 BoundFields 放在 TemplateField 下,它将无法编译。

如何在不创建额外列的情况下将页脚添加到网格视图?此外,当我们使用它时,如何将其 colspan 设置为 1? (它只会有一个更新按钮,因此页脚中不需要三列。)

配色方案:

protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[3].Text == "Match")
           e.Row.BackColor = Color.Lime;
        if (e.Row.Cells[3].Text == "Mismatch")
           e.Row.BackColor = Color.Gold;
        if (e.Row.Cells[3].Text == "New File")
           e.Row.BackColor = Color.PeachPuff;
    }
}

此方法似乎无法识别 ItemTemplate 值...

【问题讨论】:

  • 您又添加了一个模板字段,这就是为什么它会在页脚中显示一个带有按钮的额外空列。
  • @CodeRider 是的,但如果我不添加 TemplateField,它将不允许 FooterTemplate...
  • 正是您想要的?您在运行时隐藏了几列。如果您要隐藏该列,则页脚将自行隐藏。请详细说明,你想要什么。
  • @CodeRider 正如你所说,校验和和日期列在运行时被隐藏,因为它们更多地用于故障排除/调试目的。就插入数据库表的内容而言,它们也可能参与逻辑。 (自从我写那篇文章以来已经有几个星期了,所以我忘记了,我得回去检查一下。)但与此同时,我不想删除它们。我想要的只是显示页脚而不添加额外的列

标签: c# asp.net gridview


【解决方案1】:

尝试仅对最后一列使用模板字段,并在该列中指定 ItemTemplate 和 FooterTemplate。试试下面的代码。

<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
    CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
    OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
    <HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
    <FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
    <Columns>
        <asp:BoundField DataField="FOLDER" HeaderText="Location" />
        <asp:BoundField DataField="FILE" HeaderText="File" />
        <asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
        <asp:TemplateField HeaderText="Status">
             <ItemTemplate>
                  <asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("STATUS") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
     </Columns>
</asp:GridView>

我更改了 Cs 文件以从模板字段中读取值。请重新复制 ASPX,因为我也通过向标签添加 ID 进行了更改

CS:

protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label StatusLabel = e.Row.FindControl("StatusLabel") as Label;
        if (StatusLabel.Text == "Match")
           e.Row.BackColor = Color.Lime;
        if (StatusLabel.Text == "Mismatch")
           e.Row.BackColor = Color.Gold;
        if (StatusLabel.Text == "New File")
           e.Row.BackColor = Color.PeachPuff;
    }
}

【讨论】:

  • 好的,我需要在标签上添加一个右括号 > 和标记 ,并将“服务器”更改为服务器,但这有效!唯一的问题是,现在它正在显示我不想要的数据
  • 我明白你在说什么。您可以将 STATUS 转换为 TemplateField 而不是 DATE。我会更新代码,你可以复制粘贴。
  • 您是否尝试过同时使用 BoundField 和 TemplateField 作为 Status,但设置 BoundFiedl Status Visible=False。所以配色方案可以使用它,但你显示另一个。如果你愿意,我可以按我的意思更新代码?
  • 你能在 Colour_Columns 方法中发布你所拥有的内容吗?我猜这就是着色的逻辑。
  • 我刚刚更改了回复,并添加了您更改后的 CS 以使用模板字段。请重试。
【解决方案2】:

您不应将FooterTemplateBoundField 一起使用。页脚模板旨在与TemplateField 一起使用。另外,页脚模板应用于每列,这样做是为了在您有数字数据的情况下,您可以在gridview 的底部聚合总计。

这是一个在第一列中使用带有页脚字段的模板字段的示例,您可以根据需要进行更改以满足您的需要:

ASPX:

    <asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblFolder" runat="server" Text='<%# Eval("FOLDER") %>' />
                </ItemTemplate>
                <FooterTemplate>
                    Footer content displayed under FOLDER, notice no extra column!
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblFile" runat="server" Text='<%# Eval("FILE") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="lblCheck" runat="server" Text='<%# Eval("CHECKSUM") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

结果:

或者,您可以坚持使用 BoundFields 并在代码中动态添加页脚:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        var footer = new Label();
        footer.Text = "Footer content";
        //Footer will be displayed under the *first* column
        e.Row.Cells[0].Controls.Add(footer); 
   }
}

【讨论】:

  • 啊,好吧,我只是想它们也可以用作反向字幕。既然我用的是BoundFields,那我是不是应该在底部多加一行,放弃这个页脚业务呢?
  • 这取决于您,您可以在 gridview 下方添加一些控件或重新构建标记以使用 TemplateFields 而不是 BoundFields,您可以选择!
【解决方案3】:

如何编码此表单:

采购订单大师 [lblPONumberH]                      
PO Number ://here is textBox //RequireField PO Date : //这里是带有日历扩展器的文本框 供应商://这里是下拉菜单 创建者://这里是文本框 保存//按钮取消//按钮 网格视图

项目 描述 预算编号 数量 UOM 价格 数据绑定 数据绑定 数据绑定 数据绑定 数据绑定 编辑 & 删除 btn
页脚样式 txtItemDescription ddlBnumber txtQuantity ddlUOM txtPrice AddBtn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 2014-09-19
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2018-02-11
    相关资源
    最近更新 更多