2

我已经动态创建了一个gridview。设计部分中描述了模板字段。所有列都是通过后面的代码创建的,如下所示。它工作正常。在这里,我可以列出每一行的页面。但我不知道如何通过后面的代码实现页脚模板中的页面总和。

TemplateField Pages = new TemplateField();
Pages.HeaderText = "Pages";
Pages.ItemTemplate = new GridViewTemplate_Pages();
gv1.Columns.Add(Pages);


    public class GridViewTemplate_Pages : ITemplate
    {

        void ITemplate.InstantiateIn(Control container)
        {
            Label PagesLabel = new Label();
            PagesLabel.DataBinding += new EventHandler(this.PagesLabel_DataBinding);
            container.Controls.Add(PagesLabel);
        }

        void PagesLabel_DataBinding(object sender, EventArgs e)
        {
            Label lbl1 = (Label)sender;
            GridViewRow row = (GridViewRow)lbl1.NamingContainer;
            lbl1.Text = DataBinder.Eval(row.DataItem, "PagesReceived").ToString();
        }
    }

在 aspx 页面中给定 ShowFooter="True" 和 RowDataBound 分别编写。如果我在 aspx 页面中给出页脚模板但不知道如何以编程方式获取结果,则以下代码可以正常工作。请指教。

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
            TotalPages = TotalPages + RowTotalPages;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label m = (Label)e.Row.FindControl("gv1TotalPages");
            m.Text = TotalPages.ToString();
        }
    }
4

2 回答 2

1

您可以像这样为 gridview 创建页脚。

//代码

GridView gv = new GridView();
gv.RowCreated += delegate(object dsender, GridViewRowEventArgs ge)
{
    if (ge.Row.RowType == DataControlRowType.Footer)
        ge.Row.Cells[0].Text = "Something";
};
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
BoundField bf = new BoundField();
bf.HeaderText = "col 1";
bf.DataField = "Length";
gv.Columns.Add(bf);
gv.DataSource = new string[] { "One", "Two", "Three" };
gv.DataBind();
Form.Controls.Add(gv);

这适用于动态创建的网格视图和页脚。您可以进行相应的更改。

于 2013-05-22T05:19:39.053 回答
0

这是一件愚蠢的事情。我把它弄复杂了。我需要页脚行中的页面总和,那么我为什么要思考和混淆 GridViewTemplate 和 Itemplate 类。只需在会话中添加总页数并在 DataControlRowType.Footer 中重新收集它。这是代码:

    protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int RowTotalPages = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PagesReceived"));
            int RowTotalCharges = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ChargesReceived"));
            TotalPages = TotalPages + RowTotalPages;
            TotalCharges = TotalCharges + RowTotalCharges;
            Session.Add("TotalPages", TotalPages.ToString());
            Session.Add("TotalCharges", TotalCharges.ToString());
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "Batches Count: " + gv1.Rows.Count.ToString();
            e.Row.Cells[5].Text = Session["TotalPages"].ToString();
            e.Row.Cells[6].Text = Session["TotalCharges"].ToString();
        }
    }
于 2013-05-24T06:27:44.693 回答