格式化动态下拉列表文本(金钱)
本文关键字:金钱 文本 动态 下拉列表 格式化 | 更新日期: 2023-09-27 18:08:00
我试图格式化我的下拉列表的文本,但似乎没有工作。下面是我的代码:
下拉列表:
<asp:DropDownList runat="server" ID="ddlSalary" CssClass="ddlBox"
CausesValidation="true" AutoPostBack="true"
onselectedindexchanged="ddlSalary_SelectedIndexChanged" />
背后的代码:
if (!IsPostBack)
{
ddlSalary.DataSource = Placements.DropDownPopulating("Placement_Salary_From");
ddlSalary.DataBind();
ddlSalary.Items.Insert(0, new ListItem("-- Salary --", "0"));
结果:
6.0000
200.0000
1000.0000
但是我想:
£6
£200
£1000
我试过使用:
ddlSalary.DataTextFormatString = "{0:C}";
编辑
填充下拉列表:
public static List<string> DropDownPopulating(string selectedFilter)
{
List<string> returnVal = new List<string> { };
using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["websiteContent"].ConnectionString))
{
sqlCon.Open();
string SQL = "SELECT DISTINCT " + selectedFilter + " FROM Placements";
using (var CMD = new SqlCommand(SQL, sqlCon))
{
using (var DR = CMD.ExecuteReader())
{
while (DR.Read())
{
returnVal.Add(DR[selectedFilter].ToString());
}
}
}
sqlCon.Close();
}
return returnVal;
}
如果你想让格式工作,你应该使用numeric
数据类型:
public static List<T> DropDownPopulating<T>(string selectedFilter)
{
var returnVal = new List<T>();
var connectionString = ConfigurationManager.ConnectionStrings["websiteContent"].ConnectionString;
using (var con = new SqlConnection(connectionString))
using (var cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "SELECT DISTINCT " + selectedFilter + " FROM Placements";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// if you are using .NET 4.5
returnVal.Add(reader.GetFieldValue<T>(reader.GetOrdinal(selectedFilter)));
// if you are using .NET 4.0 or older:
// returnVal.Add((T)reader.GetValue(reader.GetOrdinal(selectedFilter)));
}
}
}
return returnVal;
}
然后:
ddlSalary.DataSource = Placements.DropDownPopulating<decimal>("Placement_Salary_From");
ddlSalary.DataTextFormatString = "{0:C}";
ddlSalary.DataBind();
使用这个查询获取数据.....
select '$'+Cast((Cast(Price as int))as varchar) from TableName