我得到了这个方法,我怎样才能让小数点为.00而不是.0000?ASP.NET/C#十进制到0.00
public static List<Product> GetAllProducts()
{
List<Product> products = new List<Product>();
string sqlQuery = "SELECT * FROM Products";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
Product product = new Product();
product.Id = Convert.ToInt32(reader["Id"]);
product.ManufacturerId = Convert.ToInt32(reader["ManufacturerId"]);
product.CategoryId = Convert.ToInt32(reader["CategoryId"]);
product.Name = (reader["Name"]).ToString();
product.Description = (reader["Description"]).ToString();
product.Price = Convert.ToDecimal(reader["Price"]);
product.ItemsInStock = Convert.ToInt32(reader["ItemsInStock"]);
products.Add(product);
}
}
}
}
return products;
}
更新: 对不起,要求愚蠢的问题。 我看不到的地方把DataFormatString = “{0:F2}”
这是我的网格:
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
顺便说一句,这说明了我喜欢的Visual Basic更好几件事情之一。想象一下,这是名字空间最后一堂课的最后一种方法。该文件的结尾将有6个嵌套的大括号(})。 VB结束了更容易阅读。 – 2009-10-23 18:22:00
对不起,我打印到GridView。 我不能使用product.Price = Convert.ToDecimal(reader [“Price”]。ToString(“0.00”));因为它期望一个整数 – user83713 2009-10-23 18:25:51
但我会尝试你的解决方案 – user83713 2009-10-23 18:26:23