DataGridView-如何仅设置单个列的货币格式 [英] DataGridView - how to set the currency format for a single column ONLY

查看:224
本文介绍了DataGridView-如何仅设置单个列的货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将datagridview用于篮子。我已经得到它来显示客户的购物篮,但是我希望它为价格列显示货币,但是我也有一个数量列,因此,如果我将默认样式设置为货币,则它将这两个列都更改为

I am trying to use the datagridview for a basket. I have got it to display the basket of the customer but I would like it to display the currency for the Price column, but i have a Quantity column as well, so if I put the default style as currency then it will change both columns to the currency format.

我想要做的就是将货币格式添加到价格列中,而不是数量列中。

What I want to be able to do is add the currency format to the Price Column but not to the quantity column.

以下是显示购物篮的代码(Form_load)

Here is the code that displays the basket (Form_load)

using (var con = new SqlConnection(connectionString))
        {
            SqlDataAdapter dataadapter =
          new SqlDataAdapter(
              "select p.productname 'Product Name', b.productquantity 'Quantity', c.categoryname 'Category', p.price 'Current Price' " +
              "from basket b join products p on b.productid = p.productid " +
              "join Categories c on c.categoryid = p.categoryid " +
              $"where b.customerid = {CustomerId}", con);

            DataSet ds = new DataSet();
            con.Open();
            dataadapter.Fill(ds);
            con.Close();
            dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

CustomerId是从登录时存储CustomerId的txt文件中收集的

The CustomerId is collected from a txt file that stores the CustomerId when they log in.

如果您还想查看其他代码,请发表评论,我会添加它。

If there is any more code that you would like to see then comment and i will add it.

这是我在窗体上添加的货币作为样式的内容。

This is what i get on the form with the currency added as the style.

推荐答案

您可以设置列数据的格式使用列的 DefaultCellStyle 属性的 Format 属性。

You can set format of data of a column using Format property of its DefaultCellStyle property of the column.

例如要使用当前区域性对 DataGridView 的第二列使用货币格式,可以使用以下代码:

For example to use currency format for second column of a DataGridView using current culture, you can use such code:

grid1.Columns[1].DefaultCellStyle.Format = "c";

例如,使用特定的区域性和特定的十进制数字:

Or for example to use an specific culture and specific decimal numbers:

grid1.Columns[1].DefaultCellStyle.Format = "c2";
grid1.Columns[1].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("en-GB");

更多信息

  • How to: Format Data in the Windows Forms DataGridView Control
  • Standard Numeric Format Strings - The Currency ("C") Format Specifier.

注意

如果使用的是对象数据源,则也可以使用以下方法。基础是相同的,为列设置了合适的格式,但是使用了以下属性:

If you are using an object data source, then you can use the following approaches as well. The Foundation is same, setting suitable format for the column, but using attributes:

  • DataAnnotations attributes for DataGridView in Windows Forms
  • Using custom attributes to control appearance of DataGridView columns

这篇关于DataGridView-如何仅设置单个列的货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆