0

I have a dropdown list that gets it's values from events on the database and it's working fine, this is the code that I'm using:

DataSet ds = event.ReturnFutureEvents();
meetingDropdown.DataTextField = ds.Tables[0].Columns["date"].ToString();
meetingDropdown.DataValueField = ds.Tables[0].Columns["id"].ToString();
meetingDropdown.DataSource = ds.Tables[0];
meetingDropdown.DataBind();

However, this makes the values on the dropdown formated like 02/04/2020 00:00:00, and I want only the Date, without Time. The .ToString() on these doesn't accept any arguments, and converting the columns to DateTime doesn't work.

Murilo J
  • 3
  • 1

2 Answers2

0

You can use the DataTextFormatString. In your case, you want something like this:

meetingDropdown.DataTextFormatString = "{0:dd-MM-yyyy}";

See here: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.listcontrol.datatextformatstring.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
  • Didn't work for me, it just made all the values "dd/MM/yyyy", putting it after the DataBind has no effect. – Murilo J Apr 02 '20 at 21:46
0

https://stackoverflow.com/a/17146490/2906166

Please check above answer.

  meetingDropdown.DataValueField =  ds.Tables[0].Columns["id"].ToString();
  meetingDropdown.DataTextField = ds.Tables[0].Columns["date"].ToString();
  meetingDropdown.DataTextFormatString = "{0:dd-MM-yyyy}";
  meetingDropdown.DataBind();
kAsdh
  • 356
  • 1
  • 10
  • 25