Click here to Skip to main content
15,848,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
Sergey Alexandrovich Kryukov 17-Sep-14 18:45pm    
Are you binding it with some database? If so, it would be better to perform such calculations (really simple ones) on the data source, not on UI...
Member 11070376 17-Sep-14 20:14pm    
yes i am binding to database... can u plz help me with this.

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.
Share this answer
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);

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
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900