计算GridView中每列的总值并在页脚中显示它们 [英] Calculating the total value of each column in GridView and Displaying them in Footer

查看:83
本文介绍了计算GridView中每列的总值并在页脚中显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从星期日到星期六,我的Gridview中有7列。我需要在每列中添加值并在页脚上显示它们。

我尝试为单列做但不能为每一列做。



I have 7 columns in my Gridview starting from Sunday to Saturday. I need to add the values within each column and display them on footer.
I tried doing for single column but not able to do for each column.

int total=0;
 protected void gvEmployeeTimeSheet_RowDataBound(object sender, GridViewRowEventArgs e)
        {          
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {  
                Label lblsaturday = (Label)e.Row.FindControl("lblSaturday");
                int qty = Int32.Parse(lblsaturday.Text);
                 total = total + qty;
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblTotalqty = (Label)e.Row.FindControl("lblFootersaturday");
                lblTotalqty.Text = total.ToString();
            }
        }    

推荐答案

在名为gvEmployeeTimeSheet的gridview中设置ShowFooter =True。请仔细阅读下面的代码,该代码将计算一列的总和,然后您可以将其扩展到其他列。以下代码只是您需要根据需要进行更改的示例。



In your gridview named "gvEmployeeTimeSheet" set the ShowFooter="True". Please go through the code below which is going to calculate the total of one column and you may then extend it to other columns. The below code is just a sample you will need to change it according to your needs.

decimal TotalSales = (decimal)0.0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
        TotalSales += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ProductSales"));
      else if (e.Row.RowType == DataControlRowType.Footer)
        e.Row.Cells[1].Text = String.Format("{0:c}", TotalSales);
    }





希望上面的代码示例有所帮助。



Hope the above code sample helps.


假设你的 DataGridView 有一个 DataTable 作为你可以这样做的数据源。



只是一个示例表。您已有数据表

Assuming your DataGridView has a DataTable as a data source you can do like this.

Just an example table. You already have a data table
DataTable dt = new DataTable();
dt.Columns.Add("Sunday", typeof(int));   // I have used int as data type.
dt.Columns.Add("Monday", typeof(int));
dt.Columns.Add("Tuesday", typeof(int));
dt.Columns.Add("Wednesday", typeof(int));
dt.Columns.Add("Thursday", typeof(int));
dt.Columns.Add("Friday", typeof(int));
dt.Columns.Add("Saturday", typeof(int));

// Add some sample data
for (int i = 0; i < 5; i++)
{
    dt.Rows.Add(1, 2, 3, 4, 5, 6, 7);
}
dt.AcceptChanges();





这里你的代码开始



Here your code starts

// Convert the DataTable to an IQueryable (LINQ)
var query = dt.AsEnumerable().AsQueryable();

// Calculate the sum for the different columns
// If you have another data type in your column, you need to use a different cast
int sumSunday = query.Sum(row => (int)row["Sunday"]);
int sumMonday = query.Sum(row => (int)row["Monday"]);
// And so on


这篇关于计算GridView中每列的总值并在页脚中显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆