C# 如何计算行数据绑定事件中的总计
我想把总计列的和作为一个总数。如何计算总计并在rowdatabound事件的标签中显示C# 如何计算行数据绑定事件中的总计,c#,asp.net,gridview,C#,Asp.net,Gridview,我想把总计列的和作为一个总数。如何计算总计并在rowdatabound事件的标签中显示 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gridview" DataKeyNames="Id" Width="633px" OnRowDataBound="GridView1_RowDataBound"> <Colu
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="gridview" DataKeyNames="Id" Width="633px" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="UnitPrice" />
<asp:BoundField DataField="Total" HeaderText="Total" SortExpression="Total" />
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = Convert.ToInt32(e.Row.Cells[2].Text);
int value2 = Convert.ToInt32(e.Row.Cells[3].Text);
int total = value * value2;
e.Row.Cells[4].Text = Convert.ToString(total);
int GrandTotal = +total;
lblgrandTotal = GrandTotal.ToString();
}
}
您可以在现有的
GridView1_RowDataBound
事件中执行此操作,方法是将总计添加到每行的变量中,如下所示:-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = Convert.ToInt32(e.Row.Cells[2].Text);
int value2 = Convert.ToInt32(e.Row.Cells[3].Text);
int total = value * value2;
e.Row.Cells[4].Text = Convert.ToString(total);
int GrandTotal = +total;
lblgrandTotal = GrandTotal.ToString();
}
}
int grandTotal = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = Convert.ToInt32(e.Row.Cells[2].Text);
int value2 = Convert.ToInt32(e.Row.Cells[3].Text);
int total = value * value2;
grandTotal += total;
e.Row.Cells[4].Text = Convert.ToString(total);
lblgrandTota.Text = grandTotal.ToString();
}
}
我已将
grandTotal
变量放置在事件处理程序方法之外,这样它就不会为每一行初始化。grandTotal
所有总计的内容是什么。我想把所有的总数加起来。我更新了请参见Takeint GrandTotal
受保护范围之外的内容。使其成为一个全局变量而不是局部变量。显示此变量的标签。grandTotal:System.Web.UI.WebControls.Label我想我能做到。@krishnamohan-我想grandTotal和你的grandTotal之间有点混淆。更正。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int value = Convert.ToInt32(e.Row.Cells[2].Text);
int value2 = Convert.ToInt32(e.Row.Cells[3].Text);
int total = value * value2;
e.Row.Cells[4].Text = Convert.ToString(total);
int GrandTotal = +total;
lblgrandTotal = GrandTotal.ToString();
}
}