C# get date between two dates with hours in same line

Viewed 178

In my program I have to filter the datagridview between dates but the problem is that I put a "datetime" so it puts the day/month/year + hours/minutes/seconds but I just want to filter by date, with the code I have in C# it only works if it doesn't have the hours/minutes ahead, does anyone know the process I have to do to put the dates and ignore the hour that is ahead?

C# code:

bimedicoesBindingSource.Filter = string.Format("med_data >= #{0:yyyy/MM/dd}# And med_data <= #{1:yyyy/MM/dd}#", dtpInicial.Value, dtpFinal.Value);

Before filtering:

enter image description here

After filtering: enter image description here

1 Answers

Well, I'm not very familiar with datagridview, but the problem is that a DateTime without time is equivalent to the 0h of the date. So, you're actually filtering >='2021-06-17T00:00' and <= '2021-06-17T00:00'.

Just add one day to the end date and you will be fine.

bimedicoesBindingSource.Filter = string
    .Format(
        "med_data >= #{0:yyyy/MM/dd}# And med_data < #{1:yyyy/MM/dd}#", 
        dtpInicial.Value, 
        dtpFinal.Value.AddDays(1));

Notice that I changed the end condition to < instead of <= since it's the 0h of the next day.

Related