Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用时间格式(00:00:00)对行求和并在网格视图的页脚中生成?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何使用时间格式(00:00:00)对行求和并在网格视图的页脚中生成?

C# 如何使用时间格式(00:00:00)对行求和并在网格视图的页脚中生成?,c#,asp.net,gridview,C#,Asp.net,Gridview,作为.Net的初学者,我对我的一个问题感到困惑问题是,我在一列中的内容是8:7:15、9:15:12和10:3:4我的问题是,如何添加这三行并在网格视图的页脚中生成27:25:31。我知道如何计算行的总和,但我一直坚持这种格式 任何帮助都会对我更有帮助 提前感谢。您可以尝试以下代码。使用Gridview行数据绑定事件。 在Row-Databound事件中,通过在各个组件中拆分字符串来计算总时间,如下所示 if (e.Row.RowType == DataControlRowType.Foot

作为.Net的初学者,我对我的一个问题感到困惑
问题是,我在一列中的内容是8:7:15、9:15:12和10:3:4
我的问题是,如何添加这三行并在网格视图的页脚中生成27:25:31。我知道如何计算行的总和,但我一直坚持这种格式

任何帮助都会对我更有帮助

提前感谢。

您可以尝试以下代码。使用Gridview行数据绑定事件。 在Row-Databound事件中,通过在各个组件中拆分字符串来计算总时间,如下所示

  if (e.Row.RowType == DataControlRowType.Footer)
    {
        string[] empTotalTime = new string[3] { "0", "0", "0" };
        for (int counter = 0; counter < gv.Rows.Count; counter++)
        {
            string[] singleTime = gv.Rows[counter].Cells["Your Column Index"].Text.Split(':');
            empTotalTime[0] = (Convert.ToInt32(empTotalTime[0]) + Convert.ToInt32(singleTime[0])).ToString();
            empTotalTime[1] = (Convert.ToInt32(empTotalTime[1]) + Convert.ToInt32(singleTime[1])).ToString();
            empTotalTime[2] = (Convert.ToInt32(empTotalTime[2]) + Convert.ToInt32(singleTime[2])).ToString();
        }
        ((Label)e.Row.FindControl("lblTotalTime")).Text = empTotalTime[0] +":" + empTotalTime[1] + ":" + empTotalTime[2];
    }
if(e.Row.RowType==DataControlRowType.Footer)
{
string[]empTotalTime=新字符串[3]{“0”、“0”、“0”};
对于(int counter=0;计数器
您可以尝试以下代码。使用Gridview行数据绑定事件。 在Row-Databound事件中,通过在各个组件中拆分字符串来计算总时间,如下所示

  if (e.Row.RowType == DataControlRowType.Footer)
    {
        string[] empTotalTime = new string[3] { "0", "0", "0" };
        for (int counter = 0; counter < gv.Rows.Count; counter++)
        {
            string[] singleTime = gv.Rows[counter].Cells["Your Column Index"].Text.Split(':');
            empTotalTime[0] = (Convert.ToInt32(empTotalTime[0]) + Convert.ToInt32(singleTime[0])).ToString();
            empTotalTime[1] = (Convert.ToInt32(empTotalTime[1]) + Convert.ToInt32(singleTime[1])).ToString();
            empTotalTime[2] = (Convert.ToInt32(empTotalTime[2]) + Convert.ToInt32(singleTime[2])).ToString();
        }
        ((Label)e.Row.FindControl("lblTotalTime")).Text = empTotalTime[0] +":" + empTotalTime[1] + ":" + empTotalTime[2];
    }
if(e.Row.RowType==DataControlRowType.Footer)
{
string[]empTotalTime=新字符串[3]{“0”、“0”、“0”};
对于(int counter=0;计数器
我分配三列值返回您的sql查询,并且该值分配为三个字符串,现在我们可以根据您的要求计算该值,请参见下文

        string x1 = "8:7:15";//1st column value
        string x2 = "9:15:12";//2ndcolumn value
        string x3 = "10:3:4";//3rd column value
        string[] Cx1 = x1.Split(':');
        string[] Cx2 = x2.Split(':');
        string[] Cx3 = x3.Split(':');
        string Tx1 = ((Convert.ToInt32(Cx1[0]) + Convert.ToInt32(Cx2[0]) + Convert.ToInt32(Cx3[0])) + ":" + (Convert.ToInt32(Cx1[1])
            + Convert.ToInt32(Cx2[1]) + Convert.ToInt32(Cx3[1]))) + ":" + (Convert.ToInt32(Cx1[2]) + Convert.ToInt32(Cx2[2]) + Convert.ToInt32(Cx3[2])).ToString();
最后,您可以将这个Tx1值赋给页脚单元格

更新

            string x1 = "8:7:15";//1st column value
            string x2 = "9:15:12";//2ndcolumn value
            string x3 = "10:3:4";//3rd column value
            string x4 = "1:1:1";
            string x5 = "1:1:1";

    // , I just assign 5 values,but you can assign more than lot values  in this array 
            string[] xxA = new string[5];
            xxA[0] = x1;
            xxA[1] = x2;
            xxA[2] = x3;
            xxA[3] = x4;
            xxA[4] = x5;


            string Tvalue1 = "0";
            string Tvalue2 = "0";
            string Tvalue3 = "0";
      //Now the xxA has 5 rows (No problem for how many rows ) So this foreach will round 5 time 

            for(int i = 0; i < GridView1.Rows.Count; i++)//Now xxA.Length is 5
            {
               string x = GridView1.Rows[i].Cells[9].ToString();
               int n=GridView1.Rows.Count;
               string[] xxA=new string[n]; 
                xxA[i] = x; 
      //Here is the some logic's for the calculation and formating  
                string[] Cx1 = xxA[i].Split(':');
                Tvalue1 = (Convert.ToInt32(Tvalue1) + Convert.ToInt32(Cx1[0])).ToString() ;
                Tvalue2 = (Convert.ToInt32(Tvalue2) + Convert.ToInt32(Cx1[1])).ToString() ;
                Tvalue3 = (Convert.ToInt32(Tvalue3) + Convert.ToInt32(Cx1[2])).ToString();
            }
            string FInalValue = Tvalue1 +":"+ Tvalue2 + ":"+ Tvalue3;
string x1=“8:7:15”//第1列值
字符串x2=“9:15:12”//第二列值
字符串x3=“10:3:4”//第三列值
字符串x4=“1:1:1”;
字符串x5=“1:1:1”;
//,我只分配了5个值,但在此数组中可以分配多个批次值
字符串[]xxA=新字符串[5];
xxA[0]=x1;
xxA[1]=x2;
xxA[2]=x3;
xxA[3]=x4;
xxA[4]=x5;
字符串Tvalue1=“0”;
字符串Tvalue2=“0”;
字符串Tvalue3=“0”;
//现在xxA有5行(没有多少行的问题),所以这个foreach将循环5次
对于(int i=0;i
现在,您可以在FInalValue变量中获得总的格式化值

最终代码

        string Tvalue1 = "0";
        string Tvalue2 = "0";
        string Tvalue3 = "0";
 for (int i = 0; i < 3; i++)//here 3 is gridview row length
    {
        string x = "1:1:1";//This your cell value
        int n = 3;
        string[] xxA = new string[n];
        xxA[i] = x;
        //Here is the some logic's for the calculation and formating  
        string[] Cx1 = xxA[i].Split(':');
        Tvalue1 = (Convert.ToInt32(Tvalue1) + Convert.ToInt32(Cx1[0])).ToString();
        Tvalue2 = (Convert.ToInt32(Tvalue2) + Convert.ToInt32(Cx1[1])).ToString();
        Tvalue3 = (Convert.ToInt32(Tvalue3) + Convert.ToInt32(Cx1[2])).ToString();
    }
    string FInalValue = Tvalue1 + ":" + Tvalue2 + ":" + Tvalue3;
string Tvalue1=“0”;
字符串Tvalue2=“0”;
字符串Tvalue3=“0”;
for(int i=0;i<3;i++)//这里3是gridview行长度
{
string x=“1:1:1”;//这是您的单元格值
int n=3;
字符串[]xxA=新字符串[n];
xxA[i]=x;
//下面是一些用于计算和格式化的逻辑
字符串[]Cx1=xxA[i]。拆分(“:”);
Tvalue1=(Convert.ToInt32(Tvalue1)+Convert.ToInt32(Cx1[0]).ToString();
Tvalue2=(Convert.ToInt32(Tvalue2)+Convert.ToInt32(Cx1[1]).ToString();
Tvalue3=(Convert.ToInt32(Tvalue3)+Convert.ToInt32(Cx1[2]).ToString();
}
字符串FInalValue=Tvalue1+“:”+Tvalue2+“:”+Tvalue3;

请参见:

我分配三列值返回您的sql查询,并且该值分配为三个字符串,现在我们可以根据您的需求计算该值,请参见下文

        string x1 = "8:7:15";//1st column value
        string x2 = "9:15:12";//2ndcolumn value
        string x3 = "10:3:4";//3rd column value
        string[] Cx1 = x1.Split(':');
        string[] Cx2 = x2.Split(':');
        string[] Cx3 = x3.Split(':');
        string Tx1 = ((Convert.ToInt32(Cx1[0]) + Convert.ToInt32(Cx2[0]) + Convert.ToInt32(Cx3[0])) + ":" + (Convert.ToInt32(Cx1[1])
            + Convert.ToInt32(Cx2[1]) + Convert.ToInt32(Cx3[1]))) + ":" + (Convert.ToInt32(Cx1[2]) + Convert.ToInt32(Cx2[2]) + Convert.ToInt32(Cx3[2])).ToString();
最后,您可以将这个Tx1值赋给页脚单元格

更新

            string x1 = "8:7:15";//1st column value
            string x2 = "9:15:12";//2ndcolumn value
            string x3 = "10:3:4";//3rd column value
            string x4 = "1:1:1";
            string x5 = "1:1:1";

    // , I just assign 5 values,but you can assign more than lot values  in this array 
            string[] xxA = new string[5];
            xxA[0] = x1;
            xxA[1] = x2;
            xxA[2] = x3;
            xxA[3] = x4;
            xxA[4] = x5;


            string Tvalue1 = "0";
            string Tvalue2 = "0";
            string Tvalue3 = "0";
      //Now the xxA has 5 rows (No problem for how many rows ) So this foreach will round 5 time 

            for(int i = 0; i < GridView1.Rows.Count; i++)//Now xxA.Length is 5
            {
               string x = GridView1.Rows[i].Cells[9].ToString();
               int n=GridView1.Rows.Count;
               string[] xxA=new string[n]; 
                xxA[i] = x; 
      //Here is the some logic's for the calculation and formating  
                string[] Cx1 = xxA[i].Split(':');
                Tvalue1 = (Convert.ToInt32(Tvalue1) + Convert.ToInt32(Cx1[0])).ToString() ;
                Tvalue2 = (Convert.ToInt32(Tvalue2) + Convert.ToInt32(Cx1[1])).ToString() ;
                Tvalue3 = (Convert.ToInt32(Tvalue3) + Convert.ToInt32(Cx1[2])).ToString();
            }
            string FInalValue = Tvalue1 +":"+ Tvalue2 + ":"+ Tvalue3;
string x1=“8:7:15”//第1列值
字符串x2=“9:15:12”//第二列值
字符串x3=“10:3:4”//第三列值
字符串x4=“1:1:1”;
字符串x5=“1:1:1”;
//,我只分配了5个值,但在此数组中可以分配多个批次值
字符串[]xxA=新字符串[5];
xxA[0]=x1;
xxA[1]=x2;
xxA[2]=x3;
xxA[3]=x4;
xxA[4]=x5;
字符串Tvalue1=“0”;
字符串Tvalue2=“0”;
字符串Tvalue3=“0”;
//现在xxA有5行(没有多少行的问题),所以这个foreach将循环5次
对于(int i=0;i