WinForm计算所有gridview至今
WinForm Calculate all gridview to date
我想像图像一样计算日期值。
有什么简单的方法可以在 MetroGridView 中添加像标签值一样计算的页脚?
我目前正在使用标签来显示总值。
有什么建议或想法吗?
-谢谢
代码如下:
private void btnCalc_Click(object sender, EventArgs e)
{
// check start date
if (dtStart.Value > dtEnd.Value)
{
MetroMessageBox.Show(this, "Start Date wrong!", "Error");
return;
}
DateTime Now = dtEnd.Value;
int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
DateTime PastYearDate = dtStart.Value.AddYears(Years);
int Months = 0;
for (int i = 1; i <= 12; i++)
{
if (PastYearDate.AddMonths(i) == Now)
{
Months = i;
break;
}
else if (PastYearDate.AddMonths(i) >= Now)
{
Months = i - 1;
break;
}
}
int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;
int index = metroGrid1.Rows.Add();
metroGrid1.Rows[index].Cells[0].Value = Years;
metroGrid1.Rows[index].Cells[1].Value = Months;
metroGrid1.Rows[index].Cells[2].Value = Days;
for (int i = 0; i < metroGrid1.Rows.Count; ++i)
{
Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);
// stuck here ...
lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
}
}
在我看来你应该这样声明:
lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
循环外。
而在for循环中,你用了三个Int:"Years"、"Months"、"Days"来表示时间跨度的累加,但实际上是Int,不是Timespan ,所以在循环之后,它可能是:5 年 20 个月 78 天,我认为这不是你想要的那种结果。
为什么不用TimeSpan类型来代替那三个Int?
编辑:
您可以使用以下代码:
// never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
DateTime s1 = new DateTime(1990, 1, 1);
TimeSpan ts1 = new TimeSpan();
for (int i = 0; i < metroGrid1.Rows.Count; ++i)
{
ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
}
int daysResult;
int monthsResult;
int yearsResult;
// get the total number of days, then /365 to get the year count;
yearsResult = ts1.Days /365;
// get the month count by /30 from the remainder of /365;
monthsResult = (ts1.Days % 365) / 30;
// get the day count from the remainder of /30;
daysResult = (ts1.Days % 365) % 30;
lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";
我测试了 Kuo-hsuan Hsu 解决方案 returns:
总计:19岁8个月零3天
我尝试了一些类似的东西(见下文)并得到:
总计:19岁7个月26天
Windows 计算器告诉我:19 年7 月3 周5 天和上面的一样。不过反正我的代码长期不准确,许国轩的代码也不准确
// taken values from grid
int years = 18 ;
int months = 18 ;
int days = 57 ;
DateTime today = DateTime.Today; // 2018-11-17
DateTime futuredate = today.AddYears(years);
futuredate = futuredate.AddMonths(months);
futuredate = futuredate.AddDays(days); // 2038-07-13
TimeSpan tsp = futuredate - today;
years = tsp.Days / 365;
futuredate = futuredate.AddYears(-years);
tsp = futuredate - today;
months = tsp.Days / 30;
futuredate = futuredate.AddMonths(-months);
tsp = futuredate - today;
days = tsp.Days;
string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";
我对上面的代码不满意。
我正在考虑 运行 从开始日期到结束日期的循环并计算值。
这将使我什至可以只计算工作日 - 也许有一个 public 未来 100 年假期的列表。
我想像图像一样计算日期值。
有什么简单的方法可以在 MetroGridView 中添加像标签值一样计算的页脚?
我目前正在使用标签来显示总值。
有什么建议或想法吗?
-谢谢
代码如下:
private void btnCalc_Click(object sender, EventArgs e)
{
// check start date
if (dtStart.Value > dtEnd.Value)
{
MetroMessageBox.Show(this, "Start Date wrong!", "Error");
return;
}
DateTime Now = dtEnd.Value;
int Years = new DateTime(Now.Subtract(dtStart.Value).Ticks).Year - 1;
DateTime PastYearDate = dtStart.Value.AddYears(Years);
int Months = 0;
for (int i = 1; i <= 12; i++)
{
if (PastYearDate.AddMonths(i) == Now)
{
Months = i;
break;
}
else if (PastYearDate.AddMonths(i) >= Now)
{
Months = i - 1;
break;
}
}
int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;
int index = metroGrid1.Rows.Add();
metroGrid1.Rows[index].Cells[0].Value = Years;
metroGrid1.Rows[index].Cells[1].Value = Months;
metroGrid1.Rows[index].Cells[2].Value = Days;
for (int i = 0; i < metroGrid1.Rows.Count; ++i)
{
Years += Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value);
Months += Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value);
Days += Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value);
// stuck here ...
lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
}
}
在我看来你应该这样声明:
lbltotal.Text = "Total: " + Years.ToString() + " years, " + Months.ToString() + " months and " + Days.ToString() + " days";
循环外。
而在for循环中,你用了三个Int:"Years"、"Months"、"Days"来表示时间跨度的累加,但实际上是Int,不是Timespan ,所以在循环之后,它可能是:5 年 20 个月 78 天,我认为这不是你想要的那种结果。
为什么不用TimeSpan类型来代替那三个Int?
编辑: 您可以使用以下代码:
// never mind the date of s1 (1990,1,1),it's not important, just as a start point for the Timespan ts1 to do accumulate;
DateTime s1 = new DateTime(1990, 1, 1);
TimeSpan ts1 = new TimeSpan();
for (int i = 0; i < metroGrid1.Rows.Count; ++i)
{
ts1 += s1.AddDays(Convert.ToInt32(metroGrid1.Rows[i].Cells[2].Value)).AddMonths(Convert.ToInt32(metroGrid1.Rows[i].Cells[1].Value)).AddYears(Convert.ToInt32(metroGrid1.Rows[i].Cells[0].Value)) - s1;
}
int daysResult;
int monthsResult;
int yearsResult;
// get the total number of days, then /365 to get the year count;
yearsResult = ts1.Days /365;
// get the month count by /30 from the remainder of /365;
monthsResult = (ts1.Days % 365) / 30;
// get the day count from the remainder of /30;
daysResult = (ts1.Days % 365) % 30;
lbltotal.Text = "Total: " + yearsResult.ToString() + " years, " + monthsResult.ToString() + " months and " + daysResult.ToString() + " days";
我测试了 Kuo-hsuan Hsu 解决方案 returns: 总计:19岁8个月零3天
我尝试了一些类似的东西(见下文)并得到: 总计:19岁7个月26天 Windows 计算器告诉我:19 年7 月3 周5 天和上面的一样。不过反正我的代码长期不准确,许国轩的代码也不准确
// taken values from grid
int years = 18 ;
int months = 18 ;
int days = 57 ;
DateTime today = DateTime.Today; // 2018-11-17
DateTime futuredate = today.AddYears(years);
futuredate = futuredate.AddMonths(months);
futuredate = futuredate.AddDays(days); // 2038-07-13
TimeSpan tsp = futuredate - today;
years = tsp.Days / 365;
futuredate = futuredate.AddYears(-years);
tsp = futuredate - today;
months = tsp.Days / 30;
futuredate = futuredate.AddMonths(-months);
tsp = futuredate - today;
days = tsp.Days;
string res1 = "Total: " + years.ToString() + " years, " + months.ToString() + " months and " + days.ToString() + " days";
我对上面的代码不满意。 我正在考虑 运行 从开始日期到结束日期的循环并计算值。 这将使我什至可以只计算工作日 - 也许有一个 public 未来 100 年假期的列表。