【问题标题】:Datagridview Cell Format Thousand SeparatorDatagridview 单元格格式千位分隔符
【发布时间】:2017-10-31 17:59:38
【问题描述】:

我想格式化 VB.NET datagridview 单元格以显示带有 2 个小数位的千位分隔符,如下所示 123,456,789.12 我已经尝试过的

DefaultCellStyle.Format = "N2"
DefaultCellStyle.Format = "#,###.##"

没有任何效果,它一直显示 123456789.12

注意:我正在从数据库中填写datagridview,我需要动态更改单元格格式

【问题讨论】:

  • 第二个应该是"#,###.00",但是任何一个都只能在有数字的列上工作。文本数据不会被格式化,您可能应该将其分配给数字列,这样它就不会尝试将日期格式化为那个

标签: vb.net datagridview formatting


【解决方案1】:

如果您的列类型是数字类型(例如双精度),则无论是否处于编辑模式,格式化都会起作用。 如果您的列类型是默认情况下的字符串类型,您应该处理 cellFormating 事件以在 c# 中执行如下操作:

  private void DgvCellStyle_Load(object sender, EventArgs e)

    {

        DataTable dt = new DataTable();

        dt.Columns.Add("a");

        dt.Rows.Add(155.6565);

        dt.Rows.Add(2342.2);

        this.dataGridView1.DataSource = dt;



        this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

    }



    void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

    {

        if (e.ColumnIndex == 0 && e.RowIndex != this.dataGridView1.NewRowIndex)

        {

            double d = double.Parse(e.Value.ToString());

            e.Value = d.ToString("N2");

        }

    } 

这对我有用,因为我从数据库中检索数据,并且似乎 datagridview 将其视为 nvarchar(字符串)

你可以查看这个https://social.msdn.microsoft.com/Forums/windows/en-US/95e7e7ef-2e71-412f-abe5-ffbee2c12c18/how-to-format-datagridview-columns-to-numeric-columndefaultcellstyleformat-does-not-work?forum=winformsdatacontrols

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2013-05-16
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多