为什么Gridview页脚添加了额外的列?

不敢相信我必须要问这个 – 你认为这样一个基本function很容易实现,但是我在为Gridview创建一个页脚时遇到了麻烦。 我已经查看了各种教程和其他问题,比如这里 , 这里和这里 ,但仍然遇到困难。

问题在于正确显示页脚(即不添加额外的空列)。 从我收集的内容来看,你需要将FooterTemplate放在TemplateField标签内,否则它将无法工作 – 至少它不会为我编译。 如果我在BoundFields列之后插入它,那么它会添加一个额外的列,这是不可取的。

                

在此处输入图像描述

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

如何在不创建额外列的情况下将页脚添加到gridview? 另外,当我们在它的时候,我怎么能把它的colspan设置为1? (它只会有一个Update按钮,因此页脚中不需要三列。)

色彩方案方法:

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

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

                   

我更改了Cs文件以从模板字段中读取值。 请重新复制ASPX,因为我也通过向Label添加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; } } 

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

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

ASPX:

         Footer content displayed under FOLDER, notice no extra column!               

结果:

在此处输入图像描述

或者,您可以坚持使用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); } } 

如何编码此表单:

购买订单大师[lblPONumberH]
PO编号://这里是textBox // RequireField PO Date://这里是带有Calender扩展器的文本框供应商://这里是下拉列表创建者://这里是文本框保存//按钮取消//按钮GridView

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