DataTextFormatString now working in DropDownList

Polar Bear

Newcomer
Joined
Aug 5, 2005
Messages
15
Hi,​

I don't know why but the text formatting doesn't work at all for the dropdownlist. The reader return whole integers and I want to add two quotes to simulate inches. 20 -> 20''​

I tired putting it outside the loop, and inline (html), same results.​
Visual Basic:
If dr.HasRows() Then
 
drpWidth.Items.Add(New ListItem("",""))
 
While dr.Read()
 
drpWidth.Items.Add(New ListItem(dr.GetInt32(0),dr.GetInt32(0)))
 
drpWidth.DataTextFormatString = "{0:C}"
 
End While
 
Else
drpWidth.Items.Add(New ListItem("No Data",""))
End If

Thanks​

* Um why aren't the code tags working? :/ The code is between them.​
 
Last edited by a moderator:
Well {0:C} is currency

Try:

DataTextFormatString = '{0}"'

Also you would set this property for the data list only once before the loop (after the if), not during - this is causing you to reset the DataTextFormatString for drpWidth over and over again to the same thing - it will be reset X amount of times where X is the amount rows in your reader and throughout the whole thing the value will never change so you're just wasting processor.
 
I know thats 0:C is for currency I tested '{0}"' also.
Regardless of the format I put the text is the same.

Code:
If dr.HasRows() Then
drpWidth.DataTextFormatString = "{0}''" 
drpWidth.Items.Add(New ListItem("",""))
While dr.Read()
	 drpWidth.Items.Add(New ListItem(dr.GetInt32(0),dr.GetInt32(0)))
End While
Else
drpWidth.Items.Add(New ListItem("No Data",""))
End If
 
Fixed the code block - it didn't seem to like the mixture of code and left blocks for some reason.
Also switched to the vb tags to get syntax colouring.

As to the problem itself, IIRC the DataTextFormatString property only applies when databinding, in your case as you are adding the entries manually it will not be applied; either add the strings already formatted or bind to the data reader.
 
Last edited:
Back
Top