将两个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
}