Conditional formatting in a gridView

2

I have a gridView where all the bank information is loaded from my SQLServer .

My question is this. How do I change the font color when a date is less than the system date (for example, to redirect the accounts that are due)?

Follow my code to load gridView and the screen:

private void CarregarGridContasPagarAberto()
{

    IList<ContasPagarDTO> listaContasPagar = new List<ContasPagarDTO>();
    listaContasPagar = new ContasPagarModel().CargaContasPagarAberto();

    dgvContasPagar.AutoGenerateColumns = false;
    dgvContasPagar.DataSource = listaContasPagar;
}

screen:

privatevoiddgvContasPagar_CellFormatting(objectsender,DataGridViewCellFormattingEventArgsArguments){IList<ContasPagarDTO>listaContasPagar=newList<ContasPagarDTO>();listaContasPagar=newContasPagarModel().CargaContasPagarAberto();if(this.dgvContasPagar.Columns[Arguments.ColumnIndex].Name=="DtVencContas")
    {
       // return;

       var Conta = listaContasPagar[Arguments.RowIndex];

       if(Conta.DtVencContas < DateTime.Now)
           Arguments.CellStyle.ForeColor = Color.Red;
       else
           Arguments.CellStyle.ForeColor = Color.Black;
    }
}

    
asked by anonymous 12.08.2015 / 23:42

1 answer

1

Use the CellFormating event.

private void DataGridView1_CellFormatting (object Sender, DataGridViewCellFormattingEventArgs Arguments)
{
    if (this.DataGridView1.Columns [Arguments.ColumnIndex].Name != "Sua Coluna Aqui")
        return;

    var Conta = ListaContasPagar [Arguments.RowIndex];

    if (DateTime.Now < Conta.DataDaConta)
        Arguments.CellStyle.BackColor = Color.Green;
    else
        Arguments.CellStyle.BackColor = Color.Red;
}
    
13.08.2015 / 00:13