C# 求gridview与RowDataCount之和

C# 求gridview与RowDataCount之和,c#,asp.net,gridview,C#,Asp.net,Gridview,好的,我有4列名为actual,4列名为target,我需要对所有实际列求和,并将总数传递给一个标签。。。有人能帮忙吗 到目前为止,我所知道的是不正确的 public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) //checking the index of cel

好的,我有4列名为actual,4列名为target,我需要对所有实际列求和,并将总数传递给一个标签。。。有人能帮忙吗

到目前为止,我所知道的是不正确的

public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow) //checking the index of cells and changing the font colour
            {

                System.Data.DataRowView drv = (System.Data.DataRowView) e.Row.DataItem;
                DataTable dt = drv.Row.Table;

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (dt.Columns[i].ColumnName.Contains("Actual"))
                    {
                        e.Row.Cells[i].ForeColor = Color.Red;
                    }


                    dt.Columns.Add("Total", typeof(Double));

                    foreach (DataRow row in dt.Rows)
                    {
                        int sum = row.Table.Columns<DataColumn>().Sum(dc => (int)row[dc]);
                        row.SetField("Total", sum);
                        labeltotal.Text = sum.ToString();
                    }

                }
public void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)//检查单元格索引并更改字体颜色
{
System.Data.DataRowView drv=(System.Data.DataRowView)e.Row.DataItem;
DataTable dt=drv.Row.Table;
for(int i=0;i(int)row[dc]);
行设置字段(“总计”,总和);
labeltotal.Text=sum.ToString();
}
}

您可以这样做

Int32 total = 0; 
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        total = total + Convert.ToInt32(e.Row.Cells[1].Text);
        //here you can give the column no that you want to total like e.Row.Cells[1] for column 1
        lblTotal.Text = total.ToString();
    }
}