У меня этот метод, как я могу сделать десятичное число до .00, а не .0000?ASP.NET/C# Decimal до 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;
}
UPDATE: Извините за задавать глупые вопросы. я не могу видеть, куда положить 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 становится намного легче читать. –
Извините, я печатаю это в GridView. Я не могу использовать product.Price = Convert.ToDecimal (читатель [«Цена»]. ToString («0.00»)); потому что он ожидает целое число – user83713
Но я попробую ваше решение – user83713