Datagridview cell color change not working

I have a datagridview associated with data that stores data from a database

I made a function that checks if the date matches the correct time interval

if its correct nothing happens.

otherwise it should change the color of the row / cell to red

I tried a lot of things but nothing works

Here is the method I created:


        private void CheckFactTermijn()
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DateTime FactuurDatum = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value.ToString());
                int termijn = Convert.ToInt32(dataGridView1.Rows[i].Cells[7].Value.ToString());
                DateTime finalDate = FactuurDatum.AddDays((double)termijn);

            if (finalDate > DateTime.Now)
            {
            }
            else
            {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
            }
        }
    }
+3
source share
3 answers

I'm not sure when your code works, but try setting the DataGridView DefaultCellStyle properties in your DGV DataGridView.CellFormatting event .

The MSDN link above is an example of what you are trying to do.

, DGV (, ); DataGridViewCellFormattingEventArgs ColumnIndex, , .

+1

, , , . , . , cellformatting, databindingcomplete, , , . , , datagridview , , . , , Shown(), . , , , , , .

, , / Shown() . msdn, , 3/4 .

MSDN

+3

dataGridView1.Rows [i] .DefaultCellStyle.BackColor = Color.Red;

It is necessary to change bakColor for the entire line. With the exception of cells that have their own .BackColor style .

You can set breakPoint in a sentence to verify this.

0
source

Source: https://habr.com/ru/post/1753728/


All Articles