Fill a DropDownList with Date. ASPX C #

0

Hello! How can I fill a DropDownList that stores me the last five previous dates beginning with, say, today?

06/28/2018 06/27/2018 06/25/2018

And when selecting a date I upload a GridView that is linked to a database; a table that contains a column with the date in the format 2018/06/26. I know I can do with a WHILE to match the dates and show them, but I have no idea how to load the dates in the DropDownList. Obviously these have to be updated depending on the current day.

    
asked by Elizabeth 28.06.2018 в 17:41
source

1 answer

0

Using the DateTime#AddDay method you can achieve it. This method what it does is that it adds / subtracts a day from the date and returns an instance with the new date:

DateTime.Now.AddDays(-1) // 20-06-2018

Then you would only have to add a for and add the dates to DropDownList :

for(var i = 0; i > -5;i--)
{
   fechasDropDown.Items.Add(new ListItem(DateTime.Now.AddDays(i).ToString("dd/MM/yyy")));
}
    
answered by 28.06.2018 / 17:51
source