为什么GridView的页脚增加一个额外的列? [英] Why is Gridview footer adding an extra column?

查看:90
本文介绍了为什么GridView的页脚增加一个额外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法相信我有问这个 - 你会觉得这样的一个基本特征是容易实现的,但我无法创建一个GridView页脚。我已经签出各种教程和其他问题,如这里和的这里和<一个href=\"http://stackoverflow.com/questions/12545919/how-do-i-add-footertemplate-to-gridview-with-bountfields\">here,但我还是遇到了困难。

Can't believe I have to ask this - you'd think such a basic feature would be straightforward to implement, but I'm having trouble creating a footer for a Gridview. I've checked out various tutorials and other questions, such as here, and here and here, but am still encountering difficulties.

问题是正确显示页脚(即无需添加额外的空列)。从我收集,你需要把FooterTemplate为TemplateField标签内,否则将无法正常工作 - 至少它不会编译我。如果我插入这个绑定列列之后,然后把它添加一个额外的列是不希望​​的。

The problem is in displaying the footer properly (i.e. without adding an extra empty column). From what I gather, you need to put the FooterTemplate inside a TemplateField tag or it won't work - at least it wouldn't compile for me. If I insert this after the BoundFields columns then it adds an extra column which is not desirable.

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

同样,如果我把它绑定列之前,它增加了左边一个额外的列。如果我试图把所有的绑定列的模板列下它不会编译。

Similarly, if I put it before the BoundFields it adds an extra column on the left. If I try to put all the BoundFields under the TemplateField it won't compile.

如何添加页脚到GridView,而无需创建一个额外的列?此外,虽然我们在这,我如何设置其列跨度为1? (这只是将不得不在它的人更新按钮,因此没有必要在页脚三列。)

How do I add the footer to the gridview without creating an extra column? Also, while we're at it, how can I set its colspan to 1? (It's only going to have the one Update button in it, so no need for three columns in the footer.)

颜色,模式方法:

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值...

This method does not seem to recognize ItemTemplate values...

推荐答案

尝试使用模板字段只为最后一列,并在该列中可以指定的ItemTemplate和FooterTemplate。尝试下面的code。

Try using template field only for the last column and in that column you can specify the ItemTemplate and FooterTemplate. Try the code below.

<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标签

I changed the Cs file to read the value from the template field. Please recopy the ASPX to becouse I changed it too by adding an ID to the Label

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;
    }
}

这篇关于为什么GridView的页脚增加一个额外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆