question

SigridsCarvalhoLima-8999 avatar image
0 Votes"
SigridsCarvalhoLima-8999 asked SigridsCarvalhoLima-8999 answered

Contar dias restantes entre datas em uma GridView (Vencimento ou Validade)

Ola, estou necessitando de fazer uma contagem dos dias restantes para validade dos produtos dentro de uma grid e retornar o valor na próxima coluna do registro

Ex:
Id / Produto / DataValidade / diasVencer
01 feijao 31/12/2021 ?????

Ja possuo uma rotina que muda de cor as celulas quando estao a vencer, vencidas e nao vencidas.

Code:

foreach (DataGridViewRow row in dgvProduto.Rows)
{
var atual = DateTime.Now;
var vencimento = DateTime.Parse(row.Cells["dataValidade"].Value.ToString());
var diasAntes = vencimento.AddDays(-31);

             if (atual > diasAntes && atual < vencimento)
             {
                 row.DefaultCellStyle.BackColor = Color.Yellow;
             }
             else if (atual > vencimento)
             {
                 row.DefaultCellStyle.BackColor = Color.Red;
             }
             else if (atual < vencimento)
             {
                 row.DefaultCellStyle.BackColor = Color.Green;
             }
         }

Aguardo a colaboração, obrigado


not-supported
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.

DSPatrick avatar image
0 Votes"
DSPatrick answered

Q&A forums are currently English only. I'd try asking for help over here in dedicated forums.
https://social.msdn.microsoft.com/Forums/pt-br/home
https://social.technet.microsoft.com/Forums/pt-br/home
https://answers.microsoft.com/pt-br

--please don't forget to upvote and Accept as answer if the reply is helpful--


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.

Paul-5034 avatar image
0 Votes"
Paul-5034 answered

You can compute the remaining days like this:

 var remainingDays = Math.Max(0, (expiration - DateTime.UtcNow).Days);
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.

SigridsCarvalhoLima-8999 avatar image
0 Votes"
SigridsCarvalhoLima-8999 answered

Certo e como adicionaria o valor na coluna diasVencer

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.