2012-06-27 24 views
7

我想格式化在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列 「txtcDecimal」 擁有財產(從設計師)

txtcDecimal.DefaultCellStyle.Format = "N2"; 

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

面具 「0.00 ##」 工作作爲 「N2」 只得到2位小數 這是正確舍入到小數點後兩位,但只是不喜歡我所需要的(如我在示例中所示)

如何以簡單的方式做到這一點,而不消耗許多資源?

感謝harlam357 &湯姆Garske

+0

在你的第三個例子你是故意的指定用逗號(,)來表示一個整數還是一個錯字? – harlam357

+0

請參閱我更新了我的答案以顯示它是如何在Forms中實現的。很高興看到你得到它的工作! – Tom

回答

14

要格式化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; } 
    } 
} 
+0

這不工作,更新我的問題 – ch2o

+0

謝謝你可以看到我的錯誤=) – ch2o

2

創建自定義格式。

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

來源:How to custom format data in datagridview during databinding

3

要格式化2之間小數點後4位,您可以使用自定義格式字符串。 (如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##"); 
     } 
    } 

來源: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

相關問題