问题描述
您好,
我正在使用Telerik。
我想在Gridview底部添加Footer,以便显示该列属性的总计。
我在Windows窗体上工作。
代码如下。
GridViewSummaryItem summaryItem = new GridViewSummaryItem();
summaryItem.Name =Units ;
summaryItem.Aggregate = GridAggregateFunction.Count;
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);
this.GridViewWorkInProgress.SummaryRowsBottom.Add(summaryRowItem);
这是我添加的代码,但它不会在底部包含页脚。
它会显示错误空引用异常未处理。
Hello,
I am working on Telerik.
I want to add Footer at bottom of Gridview so it can show the totals for that column's property.
I m work on Windows form.
Code is below.
GridViewSummaryItem summaryItem = new GridViewSummaryItem();
summaryItem.Name = "Units";
summaryItem.Aggregate = GridAggregateFunction.Count;
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
summaryRowItem.Add(summaryItem);
this.GridViewWorkInProgress.SummaryRowsBottom.Add(summaryRowItem);
thats code i added but it will not include footer at bottom.
it will shows error of Null reference Exception unhandled.
推荐答案
Telerik RadGrid provides a method to define aggregates on a per column basis from design time and render the results inside the respective column's footer. Aggregate calculations are supported for GridBoundColumns and GridCalculatedColumns.
Steps:-
--Subscribe to the your DataBound event of Grid
--Iterate through the rows in the underlying grid source
--Sum up the total and insert it in the respective column footer
aspx-
<telerik:RadGrid ID="RG1" runat="server">
<MasterTableView AutoGenerateColumns="False" ShowFooter="True" AllowPaging="true">
<FooterStyle BackColor="#cc6633"></FooterStyle>
<Columns>
<telerik:GridBoundColumn HeaderText="Item Count" DataField="Item Count" UniqueName="Item Count" />
</Columns>
</MasterTableView>
<PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
in C#-
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = (GridDataItem)e.Item;
int fieldValue = int.Parse(dataItem["Item Count"].Text);
total = total + fieldValue;
}
if (e.Item is GridFooterItem)
{
GridFooterItem footerItem = (GridFooterItem)e.Item;
footerItem["Item Count"].Text = "Total: " + total.ToString();
}
}
protected void RadGrid1_DataBound(object sender, System.EventArgs e)
{
DataTable gridTable = GetDataTable("SELECT * FROM [Order Details]");
foreach (DataRow row in gridTable.Rows)
{
grandTotal = grandTotal + int.Parse(row["Quantity"]);
}
GridFooterItem footerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Footer)[0];
footerItem["Item Count"].Text = footerItem["Item Count"].Text + "<br> Total for all pages: " + grandTotal.ToString();
}
这篇关于如何在Gridview中添加页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!