在我的Windows表单中,我有一个从MS Access数据库中获取的dataGridView,它有两列TaskDate和TaskTime。任务日期(数据类型 = 日期时间(仅保存日期,任务时间(数据类型 = 日期时间(仅保存时间。现在在 datagridview 单元格格式化事件中,我想根据当前日期和时间与系统日期和时间对齐的两个条件使标签可见(标签在表单加载时的可见性为假(。下面是到目前为止我尝试的代码,它不会引发错误,但它甚至不会使标签可见。急需帮助:(
foreach (DataGridViewRow row1 in dataGridView1.Rows)
{
DateTime rowtype1 = Convert.ToDateTime(row1.Cells["TaskDate"].Value);
DateTime rowtype2 = Convert.ToDateTime(row1.Cells["TaskTime"].Value);
// Time formt is dataGridView1.Columns["TaskTime"].DefaultCellStyle.Format = @"hh:mm";
if (rowtype1 == DateTime.Today && rowtype2 > DateTime.Now)
{
// label6 visibile property is false on form load
label6.Visible = true;
label6.Text = "Its time now";
}
}
如果您愿意只比较DateTime
的时间部分.比较两种DateTime
结构TimeOfDay
属性。
rowtype2.TimeOfDay > DateTime.Now.TimeOfDay