Click here to Skip to main content
16,016,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 20-Mar-14 21:14pm
v2

All you need is CellFormatting event of your DataGridView control.

C#
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;
        }
    }
}


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 :)
 
Share this answer
 
v4
Comments
honar.cs 21-Mar-14 3:35am    
thank you Marcin Kozub for your answer ... what do you mean by columnindex == 5, i have two columns for date this mean two columnindex?
Marcin Kozub 21-Mar-14 3:45am    
This means, that you must replace 5 (it was just sample number) with proper column index of your return_date column on your DateGridView component. If you have 8 columns, just enter value from 0-7. This is index of column that you want to color.
Remember that columns are numbered from 0.
Marcin Kozub 21-Mar-14 3:58am    
I've updated my solution.
honar.cs 21-Mar-14 4:13am    
ow you right thank you ... i have one more question please ... my project is for library and the borrow_date is the date that book is borrowed and the return_date is that borrower must return the book ... if the borrower does not return the book in the return_date at that time i want to make the return_date cell for that borrower red.. is your code do these or not?
Marcin Kozub 21-Mar-14 4:21am    
Did you even tried to use this code? You have all informations that you need to acomplish your task by yourself :)
You have to do it on rowdatabound event

C#
if(e.row.cells[return_date ColumnNo].text > e.row.cells[borrow_date ColumnNo].text)
{
// set background colour
}
 
Share this answer
 
Comments
Marcin Kozub 21-Mar-14 3:49am    
RowDataBound is event for Web GridView. OP clearly ask for WinForms solution.
And comparing data as text is not a good idea btw...
Downvote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900