question

OmerButt-Graphin8 avatar image
0 Votes"
OmerButt-Graphin8 asked OmerButt-Graphin8 answered

C# Winforms: How to Change the DataGridView Row Color based on Expiry Date

Its not a ASP.net Question hence no RowDataBound Event Works here in this question please answer according to Winforms DataGridView which Event handler to be used to solve this question.

I want to change the row color of DataGridView based on Expiry Date.

If the Expiry date is >= to the value of Expiry Date Column the Row Color change to Red

and If one month left to be Expired then the row color change to Yellow.

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

OmerButt-Graphin8 avatar image
0 Votes"
OmerButt-Graphin8 answered

@Viorel-1 After trying and researched I finally achieved the desired result

by using RowPrePaint Event and below is the right way:

 private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
         {
             var ExpiryDate = Convert.ToDateTime(dgv.Rows[e.RowIndex].Cells[7].Value); 
             var TodayDate = DateTime.Today;
             var Expired = ExpiryDate >= TodayDate;
             var ExpiredToBe = ExpiryDate >= TodayDate.AddDays(-30);
             if (Expired)
             {
                 dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightCoral;
             }
             else if (ExpiredToBe)
             {
                 dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Gold;
             }
         }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.