【发布时间】:2017-06-04 18:59:20
【问题描述】:
好的,伙计们,我有一个列表框,它显示绑定列表中的产品(它们是自定义类,并具有 ID、名称、价格)。我希望列表框显示项目名称和价格。列表框 (lbProductsChosen) 将“DataTextField”设置为 Name 并将 DataValueField 设置为 ID。我正在使用 PreRender 事件来检查每个项目,从绑定列表(blProducts)等中查看它的价格,它工作得很好。我得到了列表中显示的名称和价格。但是,当它显示时,尽管我使用 String.Format 对其进行了格式化,但结果仍然是一个十进制数(例如 3.20000),而且看起来很难看。有谁知道为什么它可以显示它,但没有显示它我希望它的格式。
protected void lbProductsChosen_PreRender(object sender, EventArgs e)
{
foreach (ListItem item in lbProductsChosen.Items)
{
string currentDescription = item.Text;
int convertedValue = Convert.ToInt32(item.Value);
for (int i = 0; i < blProducts.Count; i++)
{
if (blProducts[i].ProductID == convertedValue)
{
decimal ItemPrice = blProducts[i].Price;
string convertedPrice = ItemPrice.ToString();
string currentPrice = String.Format("{0:c}", convertedPrice);
string currentDescriptionPadded = currentDescription.PadRight(30);
item.Text = currentDescriptionPadded + currentPrice;
}
}
}
}
【问题讨论】: