将两个dataGridView单元格与日期值进行比较? [英] compare two dataGridView cell with date value?

查看:288
本文介绍了将两个dataGridView单元格与日期值进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的c #windows窗体中,我有一个dataGridView,用于加载表中的数据,



dataGridView有8列,其中两列有日期值borrow_date和return_date 。



如果任何return_date单元格的日期大于borrow_date单元格的日期,则要比较两列中的列,使该单元格的背景为红色或使它成为前红色。

in my c# windows form i have a dataGridView that loads the data from a table,

The dataGridView has 8 columns two of them have date value borrow_date and return_date.

I want to compare the of that two columns if the date of any return_date cell is greater that the date of borrow_date cell, make the background of that cell red or make it's fore color red.

推荐答案

你需要的只是你的DataGridView控件的CellFormatting事件。



All you need is CellFormatting event of your DataGridView control.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "return_date")
    {
        var returnDate = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells["return_date"].Value);
        var borrowDate = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells["borrow_date"].Value);
        if (returnDate > borrowDate)
        {
            e.CellStyle.BackColor = Color.Red;
            // e.CellStyle.ForeColor = Color.Red;
        }
    }
}





欲了解更多信息,请看这里:

http://msdn.microsoft.com/library/system。 windows.forms.datagridview.cellformatting%28v = vs.110%29.aspx [ ^ ]



希望它可以帮到你:)



For more information take a look here:
http://msdn.microsoft.com/library/system.windows.forms.datagridview.cellformatting%28v=vs.110%29.aspx[^]

Hope it helps you :)


你必须在rowdatabound事件上这样做



You have to do it on rowdatabound event

if(e.row.cells[return_date ColumnNo].text > e.row.cells[borrow_date ColumnNo].text)
{
// set background colour
}


这篇关于将两个dataGridView单元格与日期值进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆