如何将行与时间格式(00:00:00)相加并在网格视图的页脚中生成?

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

任何帮助对我都会更有帮助。
提前致谢。

我指定三列值返回你的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; 

现在,您可以在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; 

看这个演示 :

您可以尝试以下代码。 使用Gridview Row-Databound事件。 在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]; }