locked
calculate total time in grid view RRS feed

  • Question

  • User-775536502 posted

    gridviewhii i have grid view with 4 column from database 

    1 : Name 

    2: Start Time

    3: End Time 

    4 : Total Time 

    the fileds are Varchar and time 

    hoe i create in the grid view calculate all the total time 

    Thursday, June 23, 2016 7:32 AM

Answers

  • User-1034726716 posted

    You could calculate the total time in your query instead. Put the result in a separate field so you can just bind them directly in the grid without extra work on your side.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 23, 2016 12:24 PM
  • User-1199946673 posted

    If Starttime and Endtime are datetimes in your table Mission, you could try this:

    SelectCommand="SELECT *, EndTime-Startime AS TotalTime FROM [Mission]"

    And you can set the DataFormatString Property of the Boundfields to get a particular datetime format:

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 26, 2016 11:08 PM

All replies

  • User-1034726716 posted

    You could calculate the total time in your query instead. Put the result in a separate field so you can just bind them directly in the grid without extra work on your side.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 23, 2016 12:24 PM
  • User61956409 posted

    Hi barakkadosh,

    You could loop through GridView Rows to calculate time duration on GridView DataBound event.

    protected void mygrid_DataBound(object sender, EventArgs e)
    {
        foreach (GridViewRow row in mygrid.Rows)
        {
            //find the control and get StartTime and EndTime from GridView Row
            //calculate time duration 
        }
    }
    

    Best Regards,

    Fei Han

    Friday, June 24, 2016 8:09 AM
  • User-775536502 posted

    hi fei han 

    10x for the help 

    i donk know how to do it can you please help me with this code 

    its my first time i writ this 

    this is the html code 

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Report.aspx.cs" Inherits="Report" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="Title" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
    .style1
    {
    width: 100%;
    }
    </style>
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="ContentBody" Runat="Server">
    <table class="style1">
    <tr>
    <td>
    &nbsp;</td>
    </tr>
    <tr>
    <td>
    <table class="style1">
    <tr>
    <td>
    &nbsp;</td>
    </tr>
    <tr>
    <td>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
    CellPadding="3" CellSpacing="2" DataKeyNames="Id" DataSourceID="SqlDataSource1"
    onrowdatabound="GridView1_RowDataBound" ShowFooter="True">
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
    ReadOnly="True" SortExpression="Id" />
    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
    <asp:BoundField DataField="Mission" HeaderText="Mission"
    SortExpression="Mission" />
    <asp:BoundField DataField="StartTime" HeaderText="StartTime"
    SortExpression="StartTime" />
    <asp:BoundField DataField="EndTime" HeaderText="EndTime"
    SortExpression="EndTime" />
    <asp:BoundField DataField="Comments" HeaderText="Comments"
    SortExpression="Comments" />
    <asp:BoundField DataField="TotalTime" HeaderText="TotalTime"
    SortExpression="TotalTime" />
    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:FBMConnectionString %>"
    SelectCommand="SELECT * FROM [Mission]"></asp:SqlDataSource>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </asp:Content>

    i need new row with total of total time 

    how can i do it ?

    Sunday, June 26, 2016 6:42 AM
  • User-1199946673 posted

    If Starttime and Endtime are datetimes in your table Mission, you could try this:

    SelectCommand="SELECT *, EndTime-Startime AS TotalTime FROM [Mission]"

    And you can set the DataFormatString Property of the Boundfields to get a particular datetime format:

    https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 26, 2016 11:08 PM