简体   繁体   中英

Formatting problems with CellFormatting DataGridView Event

Here's the problem I'm experiencing: I have a DataGridView (let's call it A) where I list many records that come from a SqlDataReader . This DataGridView is populated on the SelectionChanged event of another grid. I need to paint the rows red if the date from the forth column in grid A is greater than today (newer), else it paints the row as blue .

Here's the code that I implemented in the CellFormatting event to do so:

if (Convert.ToDateTime(dgvCREListaParcelas.CurrentRow.Cells[3].Value) > DateTime.Now)
        {
            e.CellStyle.BackColor = Color.Red;
        }
        else
        {
            e.CellStyle.BackColor = Color.LightBlue;
        }

I don't know why, but whenever I click any row in the grid, or scroll down and up, the lines' colors change. Unfortunately, I'm not able to post an image, as I don't have enough points to do so!

Would anyone be able to help with this problem?

I've managed to fix this, by changing the following line in the CellFormating Event: if (Convert.ToDateTime(dgvCREListaParcelas.CurrentRow.Cells[3].Value) < DateTime.Now)

To:

if (Convert.ToDateTime(dgvCREListaParcelas.Rows[e.RowIndex].Cells[3].Value) < DateTime.Now)

Thanks for the attention!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM