4

I am retrieving date values from dataset(_dsst) and populating them in dropdown list(drpStartYear).

Since I am storing date as datetime datatype, I am getting date values as mm-dd-yyyy 00:00:00 , where 00:00:00 is the time. But I need to get date only in dd-mm-yyyy format in dropdown list. I tried using datatextformatstring, but it is not working.

Below is the code:

 drpStartYear.DataSource = _dstt.Tables["tbSettings"].DefaultView;    
 drpStartYear.DataValueField = "StartDate";    

 drpStartYear.DataTextFormatString = "{0:d}";   
 drpStartYear.DataBind();    
venerik
  • 5,612
  • 1
  • 31
  • 42
ubaid ashraf
  • 964
  • 5
  • 14
  • 43

5 Answers5

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

EDIT: Add this:

drpStartYear.DataTextField = "StartDate";
Paul Alan Taylor
  • 10,273
  • 1
  • 23
  • 41
  • I dont need time @Garath, but still above is not working, I know i need to use format as "{0:d}" , but how to set it in drop down list, that is giving me headache – ubaid ashraf Jun 17 '13 at 11:27
3

You have to set DataTextField as well -

  drpStartYear.DataValueField = "StartDate";

  drpStartYear.DataTextField = "StartDate";
  drpStartYear.DataTextFormatString = "{0:dd-MM-yyyy}";
  drpStartYear.DataBind();
Ondrej Svejdar
  • 20,356
  • 5
  • 53
  • 87
2
drpStartYear.DataTextFormatString = "{0:dd-MM-yyyy}";
drpStartYear.DataTextField = "StartDate";

or you can set this in your mark-up as follows:

<asp:DropDownList
id="drpStartYear"
runat="server"
DataTextField = "StartDate"
DataTextFormatString="{0:dd-MM-yyyy}"/>
hutchonoid
  • 32,257
  • 14
  • 95
  • 102
  • It is not working, although "{0:d}" worked perfectly for setting date format of columns in GridView, where I used DataFormatString="{0:d}" , but how to do same in case of dropdown list – ubaid ashraf Jun 17 '13 at 11:25
0

Below code worked for me, I was using DataValueField, which didnot allow me to use DataTextFormatString, So When I changed it to DataTextField, I got intended results :)

 drpStartYear.DataSource = _dstt.Tables["tbSettings"].DefaultView;    
 drpStartYear.DataTextField = "StartDate";  

 drpStartYear.DataTextFormatString = "{0:d}";   
 drpStartYear.DataBind();  
ubaid ashraf
  • 964
  • 5
  • 14
  • 43
-1

Did you try:

drpStartYear.DataTextFormatString = "0:dd/MM/yyyy HH:mm:ss";  
Anuj
  • 1,377
  • 1
  • 17
  • 28
Piotr Stapp
  • 18,790
  • 11
  • 66
  • 112