2012-06-27 4 views
7

DataGridViewで表示およびキャプチャされた小数点以下の桁数をフォーマットしたい場合は、最小桁数と小数点以下桁数があります。例えば :DataGridViewの列 "txtcDecimal" でDataGridViewで最大と最小の10進数の列を書式設定するにはどうすればよいですか?

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) 

は、(設計者)性質を有する

txtcDecimal.DefaultCellStyle.Format = "N2"; 

txtcDecimal.DefaultCellStyle.Format = "0.00##"; // IS ANSWER. I do not work for an event that interfered 

マスク "0.00 ##" "N2" として作業のみ2つの小数を取得 小数点以下2桁まで正確に丸めますが、必要なものが好きではありません。(例で示したように)

多くのリソースを消費することなく簡単に実行できますか?

おかげでカスタムフォーマッタを作成&トムGarske

+0

あなたは示すために意図的にカンマ(、)を指定しませんでした整数か、それともタイプミスですか? – harlam357

+0

Formsでどのように実装されているかを表示するために私の回答が更新されていることをご確認ください。あなたがそれが働くのを見てうれしい! – Tom

答えて

14

カスタム書式文字列を使用することができます2と小数点以下4桁の間でフォーマットします。カスタム書式文字列を使用することができます2と小数点以下4桁の間でフォーマットするには...少しさらに

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; } 
    } 
} 
+0

これは動作しません、私の質問を更新 – ch2o

+0

ありがとう私のエラーを見ることができます=) – ch2o

2

をharlam357。

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; 
    } 
} 

SOURCE:How to custom format data in datagridview during databinding

3

に行く

txtcDecimal.DefaultCellStyle.Format = "0.00##" 

。 (Harlam357によって提供されるように)

txtcDecimal.DefaultCellStyle.Format = "0.00##" 

は、私は次のような単純なコンソールアプリケーションでそれを検証:

 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 

Harlamの答えは明らかに正しいです....

更新日:これは私が実装する方法です形でそれをented:

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##"); 
     } 
    } 

SOURCE:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/95e7e7ef-2e71-412f-abe5-ffbee2c12c18/

OUTPUT:あなたの第三の例では

Example Output from application

+0

コンソールアプリケーションで動作しますが、DataGridviewの列で動作しません。 イベントでこれを試しています_CellFormating – ch2o

+0

この文字列を使用してDataGridviewで出力または表示する内容は何ですか? 「うまくいきません」は明確ではありません。 – Tom

+0

が正常に動作していない場合、マスク「0.00 ##」は「n2」として機能し、2小数点以下を得ます – ch2o