0

我正在尝试格式化下拉列表的文本,但似乎没有任何效果。下面是我的代码:

下拉列表:

<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;
    }
4

2 回答 2

3

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();
于 2013-04-07T17:39:28.400 回答
0

使用此查询获取数据.....

  select '$'+Cast((Cast(Price as int))as varchar) from TableName
于 2013-04-07T17:12:42.453 回答