作为.Net的初学者,我对我的一个问题感到困惑。
问题是,我在一列中的内容为 8:7:15、9:15:12 和 10:3:4。
我的问题是,如何添加这三行并在 Grid 视图的页脚中生成 27:25:31。我知道制作行的总和,但我坚持使用这种格式。
任何帮助都会对我更有帮助。
提前致谢。
您可以尝试如下代码。使用 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];
}
我分配三列值返回您的 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;
看这个演示: