2013-03-27 102 views
1

不敢相信我不得不问这个 - 你会认为这样一个基本的特性很容易实现,但是我无法为Gridview创建页脚。我已经检查了各种教程和其他问题,如here,和herehere,但我仍然遇到困难。为什么Gridview页脚添加一个额外的列?

问题在于正确显示页脚(即不添加多余的空列)。从我收集的内容来看,您需要将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> 

enter image description here

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

如何在不创建额外列的情况下将页脚添加到gridview?另外,当我们处理它时,我怎样才能将它的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值...

+0

您已经添加了一个模板字段,这就是为什么它显示一个额外的空的列与页脚中的按钮。 – 2013-03-27 14:01:51

+0

@CodeRider是的,但如果我不添加TemplateField它不会允许FooterTemplate ... – user1985189 2013-03-27 14:13:09

+0

正是你想要的?您在运行时隐藏了几列。如果你将隐藏列,那么页脚会隐藏自己。请详细说明,你想要什么。 – 2013-03-27 14:17:27

回答

3

尝试仅为最后一列使用模板字段,并在该列中指定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> 

我改变了铯文件读取从模板字段中的值。请重新复制到ASPX我becouse改变它也通过增加一个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; 
    } 
} 
+1

关闭Label标签的情况下显示页脚。 – 2013-03-27 14:06:50

+0

完成,谢谢 – Marseld 2013-03-27 14:11:05

+0

好吧,我需要添加一个右括号>和标签到标签,并更改“服务器”服务器,但工作!唯一的是,现在它显示的数据,我不想 – user1985189 2013-03-27 14:11:35

2

你不应该BoundField使用FooterTemplate。页脚模板意在与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> 

结果:

enter image description here

或者,你可以使用绑定列棒,在代码中动态添加页脚:

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); 
    } 
} 
+0

好的,我只是觉得它们也可以像反向标题一样使用。由于我使用的是BoundFields,我应该在底部添加另一行并放弃这个页脚业务? – user1985189 2013-03-27 14:45:11

+1

这取决于你,你可以在gridview下面添加一些控件,或者重新构建你的标记来使用TemplateFields来代替BoundFields,你的选择! – 2013-03-27 14:49:43

0

如何代码这种形式:

采购订单MASTER [lblPONumberH]
订单号://这里是文本框// RequireField 采购订单日期://这里是带日历扩展程序的文本框 供应商://这里是下拉列表 创建者://此处是文本框 //保存按钮取消//按钮 的GridView

项目说明预算号数量计量单位价格 数据绑定数据绑定数据绑定数据绑定数据绑定编辑&德尔BTN
在页脚风格 txtItemDescription ddlBnumber txtQuantity ddlUOM txtPrice AddBtn

相关问题