Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
in the grid view i need the total for each and every row how can we get in a grid view

for suppose:

Weekstartdate    MOn       Tue     Wed    thr    fri    sat  Sun   total          

03/18/2013       8         8        8      8      8     0    0       40
03/25/2013       8         8        8      8      8     0    0       40


this is the code i used to fill the data in days wise i need the total to be calculated here.

C#
foreach (ManageTimeSheetsModel objTimesheet in objManageTimeSheetsModelLst)
        {
            weekDay = (int)objTimesheet.Timesheetdate.DayOfWeek;
            switch (weekDay.ToString())
            {
                case "1":
                    objTimesheetweekdaysModel.Mon = objTimesheet.Workinghours.ToString();
                    break;
                case "2":
                    objTimesheetweekdaysModel.Tue = objTimesheet.Workinghours.ToString();
                    break;
                case "3":
                    objTimesheetweekdaysModel.Wed = objTimesheet.Workinghours.ToString();
                    break;
                case "4":
                    objTimesheetweekdaysModel.Thu = objTimesheet.Workinghours.ToString();
                    break;
                case "5":
                    objTimesheetweekdaysModel.Fri = objTimesheet.Workinghours.ToString();
                    break;
                case "6":
                    objTimesheetweekdaysModel.Sat = objTimesheet.Workinghours.ToString();
                    break;
                case "0":
                    objTimesheetweekdaysModel.Sun = objTimesheet.Workinghours.ToString();
                    objTimesheetweekdaysMdlList.Add(objTimesheetweekdaysModel);
                    objTimesheetweekdaysModel = new TimesheetweekdaysModel();
                    objTimesheetweekdaysModel.WeekDate = objTimesheet.Timesheetdate.AddDays(1).ToShortDateString();
                    break;

            }
        }
        if (objTimesheetweekdaysModel.Sun != null || objTimesheetweekdaysModel.Mon != null ||
            objTimesheetweekdaysModel.Tue != null || objTimesheetweekdaysModel.Wed != null ||
            objTimesheetweekdaysModel.Thu != null || objTimesheetweekdaysModel.Fri != null ||
            objTimesheetweekdaysModel.Sat != null)
        {
            objTimesheetweekdaysMdlList.Add(objTimesheetweekdaysModel);
        }


        return objTimesheetweekdaysMdlList;
    }


i tried by using this code it is also not working:

<pre lang="cs">protected void GridViewWeeklyTimesheet_RowCreated(object sender, GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        decimal total = Convert.ToDecimal(e.Row.Cells[1].Text) + Convert.ToDecimal(e.Row.Cells[2].Text) + Convert.ToDecimal(e.Row.Cells[3].Text) + Convert.ToDecimal(e.Row.Cells[4].Text) + Convert.ToDecimal(e.Row.Cells[5].Text) + Convert.ToDecimal(e.Row.Cells[6].Text) + Convert.ToDecimal(e.Row.Cells[7].Text);

        ((Label)GridViewWeeklyTimesheet.FindControl("TotalHours")).Text = Convert.ToString(total);

    }

}
Posted
Updated 20-Mar-13 1:31am
v6
Comments
frostcox 20-Mar-13 4:46am    
C# or Vb?
Dhritirao's 20-Mar-13 4:54am    
C#
Nandakishore G N 20-Mar-13 5:17am    
use gridview rowdatabound..event

Alter your SP or select Query. Get the total from Sql and bind it in FrontEnd. Try this Select Statement:
SQL
SELECT Mon, Tue, Wed, Thr, Fri, Sat, Sun, (Mon+Tue+Wed+Thr+Fri+Sat+Sun) AS 'Total' FROM YourTableName

Bind the Gridview using this data.


--Amit
 
Share this answer
 
C#
protected void GridViewWeeklyTimesheet_RowCreated(object sender, GridViewRowEventArgs e)
   {

       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           if (e.Row.Cells[1].Text != "")
           {

               decimal total = Convert.ToDecimal(e.Row.Cells[1].Text) + Convert.ToDecimal(e.Row.Cells[2].Text) + Convert.ToDecimal(e.Row.Cells[3].Text) + Convert.ToDecimal(e.Row.Cells[4].Text) + Convert.ToDecimal(e.Row.Cells[5].Text) + Convert.ToDecimal(e.Row.Cells[6].Text) + Convert.ToDecimal(e.Row.Cells[7].Text);

               ((Label)GridViewWeeklyTimesheet.FindControl("TotalHours")).Text = Convert.ToString(total);
           }
       }
   }



design part:
XML
<asp:TemplateField HeaderText="TotalHours">
                    <ItemTemplate>
                  <asp:Label runat="server" ID="lblWeekTotalHrs" Text='<%# Convert.ToDecimal(Eval("Mon")) + Convert.ToDecimal(Eval("Tue")) + Convert.ToDecimal(Eval("Wed")) + Convert.ToDecimal(Eval("Thu")) + Convert.ToDecimal(Eval("Fri")) + Convert.ToDecimal(Eval("Sat")) + Convert.ToDecimal(Eval("Sun"))%>' />

              </ItemTemplate>
          </asp:TemplateField>
 
Share this answer
 
calculate total in your source which you bind with gridview... it is more easy.. if your dont want this then you can use RowDataBound event of gridview, in that event check whether the RowType is DataRow if yes then calculate the sum and bind the value to your total column...
 
Share this answer
 
You can do this two ways, you can add the total row directly to your datasource, if it's a datatable add an extra row and foreach row get a culimative total and add it that way, or if you want it in the footer of your grid in the databound event add the total at the index of each column you need. If you want a sample I will provide it.
 
Share this answer
 
Comments
Dhritirao's 20-Mar-13 7:32am    
i dont want to use any footer if i place the footer i need to keep in each and every row
Dhritirao's 20-Mar-13 7:33am    
can u provide me the sample then i can check why i m getting the error
 
Share this answer
 
Calculating sum of all days will be good practice otherwise you can also do this on

GridViewId_RowDataBound event
 
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