以下是我的gridview:
<asp:GridView ID="gv_TotalAllReg" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_TotalAllReg_RowDataBound" DataKeyNames="dt" CssClass="table table-bordered table-striped">
<Columns>
<asp:TemplateField HeaderText="Sno" HeaderStyle-BackColor="#f1f1f1">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="dt" DataFormatString="{0:MM-dd-yyyy}" HeaderText="Date" />
<asp:TemplateField HeaderText="New Registrations">
<ItemTemplate>
<asp:Label ID="lbl_Registrations" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cumulative Registrations">
<ItemTemplate>
<asp:Label ID="lbl_CumulativeRegistrations" runat="server"></asp:Label>
</ItemTemplate>
</Columns>
</asp:GridView>
我正在显示日期明智的新注册,但我需要基于新注册的累积注册:
Sno Date New Registrations Cumulative Registrations
1 12-23-2016 2
2 12-24-2016 6
3 12-25-2016 1
我需要输出如下,累积注册意味着我已经为2016年12月23日至25日的显示日期创建了一个功能。所以根据日期获得新的注册,我也希望显示基于新注册的累积注册,基于新的注册添加暨注册以及如何在RowDataBound中实现暨注册。
Sno Date New Registrations Cumulative Registrations
1 12-23-2016 2 2
2 12-24-2016 6 8
3 12-25-2016 1 9
下面是我的RowDataBound代码。
protected void gv_TotalAllReg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// e.Row.Cells.add(count.tostring());
string Registration = string.Empty, Innovation = string.Empty;
string daildate = gv_TotalAllReg.DataKeys[e.Row.RowIndex].Values[0].ToString();
bo.Para1 = daildate;
DataTable dt = bl.Get_DailyReport(bo);
((Label)e.Row.FindControl("lbl_Registrations")).Text = dt.Rows[0]["newregistrations"].ToString();
// my cum reg label ((Label)e.Row.FindControl("lbl_CumulativeRegistrations")).Text
}
}
我在gridview中为日期列编写了单独的函数,并在页面加载时调用了该方法。
答案 0 :(得分:1)
我认为你正在寻找这样的东西。它使用OnRowDataBound
事件将当前行注册计数添加到总计中,并将当前总计写入行中的标签。
int total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the current row to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//add the registrations to the total
total += Convert.ToInt32(row["Registrations"]);
//find the label in the row with findcontrol and cast it back to one
Label label = e.Row.FindControl("lbl_CumulativeRegistrations") as Label;
//fill the label with the current total
label.Text = string.Format("{0:N0}", total);
}
}