How to compare two dates in Datagridview and change color when VB.Net expires

Asked

Viewed 327 times

0

My code

Private Sub CadastroDataGridView_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles CadastroDataGridView.CellFormatting
If Me.CadastroDataGridView.Columns(e.ColumnIndex).Name = "DataGridViewTextBoxColumn15" Then
    If e.Value IsNot Nothing Then
        Dim dgvdate As Date = CDate(e.Value)
        If dgvdate < CDate(Now) Then
            e.CellStyle.BackColor = Color.Red
            e.FormattingApplied = True
        Else
            e.CellStyle.BackColor = Color.Green
            e.FormattingApplied = True
        End If

    End If
    End If

End Sub

  • Explain better, do you want to do this in load or by event? Put the code like this populating the datagrid, usually I do a part routine after popular the grid, then compare the data of the object I want popular and format the grid in this method.

  • I want to do it in Load, as soon as the user logs automatically has to color all expired tables. @Ezequieldasilvadaniel

  • Where does "Query" come from? @Ezequieldasilvadaniel

1 answer

1

Must be something like:

For Each linha As DataGridViewRow In DataGridView_consulta.Rows
     valor_celula = linha.Cells(2).Value

     If valor_celula = "Aberto" Then
          linha.Cells(2).Style.BackColor = Color.Red
End If

Just replace the comparison by date and put in a method being called in the load. The idea is to traverse the datagrid lines, take the value of the cell, which is a fixed column and arrow the style of it.

  • Thank you, I’ll try and say something

  • Ezequiel, I’m an amateur programmer, could you explain this to me?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.