我得到了這個方法,我怎樣才能讓小數點爲.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