问题描述
private void dataGridView1_CellFormatting(object sender ,DataGridViewCellFormattingEventArgs e)
      {
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DateTime hora2 = Convert.ToDateTime(textBox2.Text);
       试试
        {
            var row = dataGridView1.Rows [e.RowIndex];
            if(Convert.ToDateTime(row.Cells [" fechhora"]。Value)> = hora2)
            {
                e.CellStyle.ForeColor = Color.White;
                e.CellStyle.BackColor = Color.Red;
            }¥b $ b         }¥b $ b         catch(例外情况)
        {
            MessageBox.Show(ex.Message);
        }
DateTime hora2 = Convert.ToDateTime(textBox2.Text);
try
{
var row = dataGridView1.Rows[e.RowIndex];
if (Convert.ToDateTime(row.Cells["fechhora"].Value) >= hora2)
{
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Red;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
victor jamanca
victor jamanca
推荐答案
比较您可以使用的两个数据时间
DateTime.Compare Method(DateTime,DateTime)。
Compare two datatime you can use DateTime.Compare Method (DateTime, DateTime).
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DateTime d = DateTime.Now;
var row = dataGridView1.Rows[e.RowIndex];
//Target to the specified column
if (dataGridView1.Columns[e.ColumnIndex].Name.ToString()== "CreatTime")
{
//Target to the specified cell
if (DateTime.Compare(Convert.ToDateTime(row.Cells[e.ColumnIndex].Value), d) > 0)
{
e.CellStyle.ForeColor = Color.White;
e.CellStyle.BackColor = Color.Red;
}
}
}
希望它对您有所帮助。
最好的问候,
Bob
Bob
这篇关于您好,我需要帮助...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!