在gridview页脚C#中显示总小时数 [英] display total hours in gridview footer C#

查看:49
本文介绍了在gridview页脚C#中显示总小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有这个小问题,因为我还是C#的新手

我需要在GridView的页脚中显示工作的总小时数,并且所有字段都是自动生成的单元格[7].


Hi there

I have this little problem as I am still VERY new to c#

I need to show the total hours worked in the footer of my GridView and all the fields are auto generated cell no [7].


INDX | CLIENTNAME | PROJECTNAME | NAME |  DATE     |   START  |  END     | HOURS_WORKED
  1  | ME         | ProjTest    |aj    |06/27/2012 | 14:10:33 | 12:07:33 |   21:57:00









private void DoGridQuery() {
      string userid = GetUserID();
      string projectid = GetProjectID();
      string clientno = GetClientNO();


      string s = null;


      s = "SELECT A.INDX, B.CLIENTNAME, C.PROJECTNAME, D.NAME,CONVERT(VARCHAR(10), A.CDATE,101)[DATE],CONVERT(TIME(0), A.START_TIME,108)[START],CONVERT(TIME(0), A.END_TIME,108)[END],CONVERT(TIME(0),(A.END_TIME - A.START_TIME ),108)[HOURS_WORKED]";
      s += "FROM LOGSHEET A, CLIENTS B, PROJECTS C, DEVELOPERS D ";
      s += "WHERE D.USERID = @p2 ";
      if ((DropDownList1.SelectedValue != String.Empty) && (DropDownList1.SelectedValue != "...")) {
        s += "AND B.CLIENTNO = @p0 ";
      }
      if ((DropDownList2.SelectedValue != String.Empty) && (DropDownList2.SelectedValue != "...")) {

          s += "AND C.PROJECTID = @p1 ";
      }

      String[] pr = new String[3];
      pr[0] = clientno;
      pr[1] = projectid;
      pr[2] = userid;

      GridView1.DataSource = cQ.CreateDataSet(s, pr);

      GridView1.DataBind();


      GridView1.Visible = true;
    }





这是我的GridView源代码



and this is my GridView source

<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand"

             ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:ButtonField CommandName="Stop" Text="Stop" />
            </Columns>

            <HeaderStyle BackColor="#006600" ForeColor="White" />
        </asp:GridView>



任何帮助将不胜感激
在此先感谢



ANY help would be appreciated
thanks in advance

推荐答案


检查此
Hi ,
Check this
DateTime dt = new DateTime();
   TimeSpan ts;
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           //here make your calculation by using TimeSpan .
           //ts = dt - dt;
           //ts.TotalHours;
           dt = Convert.ToDateTime(e.Row.Cells[0].Text);
       }
       else if (e.Row.RowType == DataControlRowType.Footer)
       {
           //here you put your final result
           e.Row.Cells[0].Text = ts.TotalMinutes; ;
       }
   }


最好的问候
M.Mitwalli


Best Regards
M.Mitwalli


在下面的链接中进行了类似的讨论,希望对您有所帮助:
http://forums.asp.net/t/1119102.aspx [ http: //www.asp.net/web-forms/tutorials/data-access/custom-formatting/displaying-summary-information-in-the-gridview-s-footer-cs [ http://csharpdotnetfreak.blogspot.in/2009/07/display-total- in-gridview-footer.html [ ^ ]
Have a look at below links with similar discussions, hope it helps:
http://forums.asp.net/t/1119102.aspx[^]
http://www.asp.net/web-forms/tutorials/data-access/custom-formatting/displaying-summary-information-in-the-gridview-s-footer-cs[^]
http://csharpdotnetfreak.blogspot.in/2009/07/display-total-in-gridview-footer.html[^]


最好的和最简单的解决方案是创建一个timediff字段[TIME_SPENT]"datetime"和一个分钟字段[MINUTES]在SQL中为"int",然后通过运行和更新查询

honestly the best and easiest solution for me was to create a timediff field[TIME_SPENT] "datetime" and also a minutes field [MINUTES] as "int" in SQL then by running and update query

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Stop") {
        int row = int.Parse(e.CommandArgument.ToString());
        string indx = GridView1.Rows[row].Cells[1].Text;
        string s = null;
        s = "UPDATE LOGSHEET ";
        s += "SET TIME_SPENT = DATEADD(MINUTE, DATEDIFF(MINUTE,START_TIME, END_TIME), 0) ";
        s += "WHERE INDX = @p0 ";


        String[] pr = new String[1];
        pr[0] = indx;

        cQ.execSql(s, pr);
        DoGridQuery();
      }

if (e.CommandName == "Stop") {
        int row = int.Parse(e.CommandArgument.ToString());
        string indx = GridView1.Rows[row].Cells[1].Text;  
        string s = null;
        s = "UPDATE LOGSHEET ";
        s += "SET MINUTES = (DATEPART(HOUR,TIME_SPENT)*60)+(DATEPART(MINUTE,TIME_SPENT)) ";
        s += "WHERE INDX = @p0 ";


        String[] pr = new String[1];
        pr[0] = indx;

        cQ.execSql(s, pr);
        DoGridQuery();
      }
   }



所以你的桌子看起来像

日志表
========



So your table will look like

LOGSHEET
========

INDX|CLIENTNAME|PROJECTNAME|NAME| DATE     |  START | END    |TIME_SPENT|MINUTES|
  1 |ME        |ProjTest   |aj  |06/27/2012|14:10:00|13:10:00| 01:00:00 |60     |





protected string GetTotal() {
      string userid = Session["id"].ToString();
      string s = null;


      s = "SELECT SUM(MINUTES) ";
      s += "FROM LOGSHEET ";
      s += "WHERE USERID = @p0 ";

      String[] pr = new String[1];
      pr[0] = userid;

      return cQ.GenQuery(s, pr);
    }



然后就



AND THEN JUST

Label5.Text = GetTotal();



但是那又是我吗?!? ;)

如果有人有其他方法可以随意发布,我很乐于学习新方法



But then again that''s just me?!? ;)

if anyone has other ways feel free to post them I''m keen on learning new ways


这篇关于在gridview页脚C#中显示总小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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