2012-06-27 34 views
7

Tôi muốn định dạng các vị trí thập phân được hiển thị và được ghi lại trong một DataGridView, tôi có số chữ số thập phân tối thiểu và số chữ số thập phân tối đa. Ví dụ:Làm thế nào để định dạng một cột có số thập phân với max và min trong 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) 

Trong cột DataGridView "txtcDecimal" có tính chất (từ nhà thiết kế)

txtcDecimal.DefaultCellStyle.Format = "N2"; 

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

mặt nạ "0.00 ##" tác phẩm như "n2" chỉ nhận được 2 số thập phân mà làm tròn chính xác đến hai chữ số thập phân nhưng chỉ không thích những gì tôi cần (như tôi hiển thị trong ví dụ)

Làm thế nào tôi có thể làm điều đó một cách đơn giản mà không cần tốn nhiều tài nguyên?

Cảm ơn harlam357 & Tom Garske

+0

Trong ví dụ thứ ba của bạn, bạn đã chỉ định một dấu phẩy (,) trên mục đích để biểu thị một toàn bộ số hay là một lỗi đánh máy? – harlam357

+0

Vui lòng xem rằng tôi đã cập nhật câu trả lời của mình để cho biết cách nó được triển khai trong Biểu mẫu. Vui mừng khi thấy bạn có nó để làm việc! – Tom

Trả lời

14

Để định dạng từ 2 đến 4 chữ số thập phân nơi bạn có thể sử dụng một chuỗi định dạng tùy chỉnh.

txtcDecimal.DefaultCellStyle.Format = "0.00##" 

Để đi một chút nữa ...

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

này Không hoạt động, cập nhật câu hỏi của tôi – ch2o

+0

cảm ơn có thể thấy lỗi của tôi =) – ch2o

2

Tạo một định dạng tùy chỉnh.

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

NGUỒN:How to custom format data in datagridview during databinding

3

Để định dạng từ 2 đến 4 chữ số thập phân nơi bạn có thể sử dụng một chuỗi định dạng tùy chỉnh. (Như được cung cấp bởi Harlam357)

txtcDecimal.DefaultCellStyle.Format = "0.00##" 

tôi xác minh nó với các ứng dụng giao diện điều khiển đơn giản sau đây:

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

Kết quả là: Câu trả lời

13.10 
13.1235 

Harlam là rõ ràng đúng ....

CẬP NHẬT: Đây là cách tôi triển khai ented nó trong các hình thức:

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

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

OUTPUT:

Example Output from application

+0

trong bảng điều khiển ứng dụng hoạt động nhưng trong cột DataGridview không hoạt động. Tôi đang thử điều này trong trường hợp _CellFormating – ch2o

+0

Nó xuất hoặc hiển thị trong DataGridview bằng chuỗi này? "không hoạt động" không rõ ràng. – Tom

+0

không hoạt động bình thường, mặt nạ "0.00 ##" hoạt động như "n2" chỉ nhận được 2 số thập phân – ch2o

Các vấn đề liên quan