在asp.net gridview中根据特定的行ID查找总和

时间:2018-02-24 14:49:04

标签: asp.net gridview

我有一个asp.net Gridview,它有以下列和行。图片已附上

enter image description here

我正在尝试根据特定Item ID查找数量总和。在这种情况下,57,53& 91.

1 个答案:

答案 0 :(得分:0)

您可以使用RowDataBound事件。

//create a variable outside the rowdatabound method
int total_sum = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the row back to a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //or if a list<object> is bound to the gridview
        //MyClass row = e.Row.DataItem as MyClass;

        //cast the correct column to an int and add it to the total
        total_sum += Convert.ToInt32(row["itemid"]);
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        //show results. this wil work even if the footer is not shown
        Label1.Text = string.Format("{0:N0}", total_sum);
    }
}