Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I need to find subtotal and total of datetime column in gv,i could do for the

integer values but find diff for datetime, can anyone plz help me


Iam having template field for datetime column

here is my piece of code:
TimeSpan totalsum;
int tohrs, tomin;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                nric= (DataBinder.Eval(e.Row.DataItem, "Nric").ToString());
                double tmpTotal = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "OTAmount").ToString());
                Total += tmpTotal;
                GrTotal += tmpTotal;



                TimeSpan t = new TimeSpan();

                foreach (GridViewRow row in GridView1.Rows)
                {
                    TimeSpan ts = TimeSpan.Parse(row.Cells[6].Text);

                   // t = t + ts;
                    tohrs = ts.Hours;
                    tomin = ts.Minutes;

                }
               
                totalsum = TimeSpan.Parse(tohrs.ToString());
            }
if (e.Row.RowType == DataControlRowType.Footer)
            {

                Label lblTotalqty = (Label)e.Row.FindControl("lblfota");
                lblTotalqty.Text = GrTotal .ToString();




                Label fr = (Label)GridView1.FooterRow.FindControl("lblfot");
                fr.Text = totalsum.ToString();
            }
} 
Posted
Updated 27-Sep-14 10:04am
v3
Comments
Where is the exact issue in code?
saisrid 30-Sep-14 11:33am    
Sorry for the delayed reply, the issue is in the rowindex, now I have got it solved.Thankyou for your response.
Please add one Solution.
saisrid 30-Sep-14 11:57am    
In the rowcreated I have the following code

bool Row = false;
if ((id != null) && (DataBinder.Eval(e.Row.DataItem, "id") != null))
{


if (id != (DataBinder.Eval(e.Row.DataItem, "id").ToString()))

Row = true;


}
if ((id != null) && (DataBinder.Eval(e.Row.DataItem, "id") == null))
{
Row = true;
rowIndex = 0;
}

if (Row)
{


GridView GridView1 = (GridView)sender;
GridView1.HeaderRow.BackColor = System.Drawing.Color.Orange;
GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
NewTotalRow.Font.Bold = true;
TableCell HeaderCell1 = new TableCell();
HeaderCell1.ColumnSpan = 6;
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell1 = new TableCell();
HeaderCell1.HorizontalAlign = HorizontalAlign.Right;
HeaderCell1.Text = tohrs.ToString();
NewTotalRow.Cells.Add(HeaderCell1);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
rowIndex++;
tohrs = 0;


}
/\jmot 30-Sep-14 2:31am    
why you need to total/sub total a date time column??

1 solution

Adding the solution given by OP hiself/herself.

In the RowCreated I have the following code
C#
bool Row = false;
if ((id != null) && (DataBinder.Eval(e.Row.DataItem, "id") != null))
{
    if (id != (DataBinder.Eval(e.Row.DataItem, "id").ToString()))
     
    Row = true;
    }
    
    if ((id != null) && (DataBinder.Eval(e.Row.DataItem, "id") == null))
    {
        Row = true;
        rowIndex = 0;
    }
     
    if (Row)
    {
        GridView GridView1 = (GridView)sender;
        GridView1.HeaderRow.BackColor = System.Drawing.Color.Orange;
        GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
        NewTotalRow.Font.Bold = true; 
        TableCell HeaderCell1 = new TableCell();
        HeaderCell1.ColumnSpan = 6;
        NewTotalRow.Cells.Add(HeaderCell);
        HeaderCell1 = new TableCell();
        HeaderCell1.HorizontalAlign = HorizontalAlign.Right;
        HeaderCell1.Text = tohrs.ToString();
        NewTotalRow.Cells.Add(HeaderCell1);
        GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + rowIndex, NewTotalRow);
        rowIndex++;
        tohrs = 0;
    }
}
 
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