问题
I currently have :
<asp:GridView ID="BalanceCheckDataGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField ItemStyle-Width="30%" DataField="Company" HeaderText="Company" />
<asp:BoundField ItemStyle-Width="30%" DataField="Balance" HeaderText="Balance" />
</Columns>
</asp:GridView>
Now I want to grab information from the balance, and sum them up, then display it ( can be at the end a row like "Total: sumValue" or a label where its text can be it's sum value)
I am new to this, Could you help please.
Thanks a lot.
回答1:
Int32 tot = 0;
protected void Dg_Source_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
tot = tot + Convert.ToInt32(e.Row.Cells[1].Text);
lblSum.Text = tot.ToString();
}
}
回答2:
Put a footer on the grid and then you could do something like this in your code behind after your grid is bound.
((Label)your_grid.FooterRow.Cells[1].FindControl("your_label_to_diplay_total")).Text = "Total:" + ds.Tables[0].Compute("sum(your_balance_field)", "").ToString();
This is off the top of my head. You will have to customize it to your application.
回答3:
int sum=0;
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label salary = (Label)e.Row.FindControl("Label3");//take lable id
sum +=int.Parse(salary.Text);
lblsum.Text = sum.ToString();
}
}
来源:https://stackoverflow.com/questions/20251987/gridview-column-sum-value-and-display-on-webpage-asp-net