C# 在DataGridView中,如何将数字十进制的列格式化为max和min?

C# 在DataGridView中,如何将数字十进制的列格式化为max和min?,c#,.net,winforms,string,datagridview,C#,.net,Winforms,String,Datagridview,我想格式化DataGridView中显示和捕获的小数位数,我有最小小数位数和最大小数位数。 例如: If caught, "120.0" display "120.00" If caught "120.01" display "120.01" If caught "120,123" display "120,123" If caught "120.1234" display "120.1234" If caught "120.12348" display "120.1235" (round)

我想格式化DataGridView中显示和捕获的小数位数,我有最小小数位数和最大小数位数。 例如:

If caught, "120.0" display "120.00"
If caught "120.01" display "120.01"
If caught "120,123" display "120,123"
If caught "120.1234" display "120.1234"
If caught "120.12348" display "120.1235" (round)
在DataGridView列中,“txtcDecimal”具有属性(来自设计器)

掩码“0.00###”与“n2”一样只能得到2位小数 哪种方法可以正确地四舍五入到小数点后两位,但不符合我的要求(如我在示例中所示)

我如何能在不消耗大量资源的情况下以简单的方式完成它


感谢harlam357和Tom Garske创建自定义格式化程序

public class MyFormatProvider : IFormatProvider, ICustomFormatter  
{  
   public object GetFormat(Type formatType)  
   {  
     if (formatType == typeof(ICustomFormatter))  
        return this;  
     else  
        return null;  
   }  

   public string Format(string format, object arg, IFormatProvider formatProvider)  
   {  
     // Check whether this is an appropriate callback               
     if (!this.Equals(formatProvider))  
        return null;  

     //if argument/ value is null we return empty string  
     if (arg == null)  
        return null;  

     string resultString = arg.ToString();  

     //transform resultString any way you want (could do operations based on given format parameter)  

     //return the resultant string  
     return resultString;  
   }  
}  

来源:

要设置小数点后2到4位之间的格式,可以使用自定义格式字符串

txtcDecimal.DefaultCellStyle.Format = "0.00##"
再往前走一点

public partial class Form1
{
   public Form1()
   {
      InitializeComponent();

      var list = new List<Data>();
      list.Add(new Data { Prop1 = 1, Prop2 = 1.2 });
      list.Add(new Data { Prop1 = 2, Prop2 = 1.234567 });

      dataGridView1.Columns.Add("Prop1", "Prop1");
      dataGridView1.Columns["Prop1"].DataPropertyName = "Prop1";
      dataGridView1.Columns.Add("Prop2", "Prop2");
      dataGridView1.Columns["Prop2"].DataPropertyName = "Prop2";
      dataGridView1.Columns["Prop2"].DefaultCellStyle.Format = "0.00##";
      dataGridView1.DataSource = list;
   }

   class Data
   {
      public int Prop1 { get; set; }
      public double Prop2 { get; set; }
   }
}
公共部分类表单1
{
公共表格1()
{
初始化组件();
var list=新列表();
添加(新数据{Prop1=1,Prop2=1.2});
添加(新数据{Prop1=2,Prop2=1.234567});
dataGridView1.Columns.Add(“Prop1”、“Prop1”);
dataGridView1.Columns[“Prop1”]。DataPropertyName=“Prop1”;
dataGridView1.Columns.Add(“Prop2”、“Prop2”);
dataGridView1.Columns[“Prop2”]。DataPropertyName=“Prop2”;
dataGridView1.Columns[“Prop2”].DefaultCellStyle.Format=“0.00##”;
dataGridView1.DataSource=列表;
}
类数据
{
公共int Prop1{get;set;}
公共双属性2{get;set;}
}
}

要设置小数点后2到4位之间的格式,可以使用自定义格式字符串。(由Harlam357提供)

我用以下简单的控制台应用程序验证了它:

        double myDouble = 13.1;
        double myDouble2 = 13.12345;
        Console.WriteLine(myDouble.ToString("0.00##"));
        Console.WriteLine(myDouble2.ToString("0.00##"));
        Console.Read();
结果是:

13.10
13.1235
哈拉姆的回答显然是正确的

更新:我就是这样在表单中实现它的:

    public Form1()
    {
        InitializeComponent();

        DataTable dt = new DataTable();
        dt.Columns.Add("a");
        dt.Rows.Add(123.4);
        dt.Rows.Add(123.45);
        dt.Rows.Add(123.456);
        dt.Rows.Add(123.4567);
        dt.Rows.Add(123.45678);
        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("0.00##");
        }
    } 
来源:

输出:


在第三个示例中,您是有意指定逗号(,)来表示整数还是输入错误?请注意,我更新了我的答案,以说明它是如何在表单中实现的。很高兴看到你成功了!控制台中的应用程序工作,但DataGridview列中的应用程序不工作。我正在尝试在事件_cellformating中使用此字符串在DataGridview中输出或显示什么?“不工作”不清楚。工作不正常,掩码“0.00###”作为“n2”工作时只得到2位小数谢谢,我真的很喜欢你的示例,我错了,在2000行代码之间,我看到另一个事件正在干扰。有时很容易丢失它。:)很高兴你能让它工作。在这个问题上我也学到了很多,很棒的问题!
13.10
13.1235
    public Form1()
    {
        InitializeComponent();

        DataTable dt = new DataTable();
        dt.Columns.Add("a");
        dt.Rows.Add(123.4);
        dt.Rows.Add(123.45);
        dt.Rows.Add(123.456);
        dt.Rows.Add(123.4567);
        dt.Rows.Add(123.45678);
        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("0.00##");
        }
    }