2016-11-06 114 views
0

我想格式化VB.NET datagridview單元格以顯示千位分隔符與2位小數這樣 123,456,789.12 什麼DataGridView的單元格格千位分隔符

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

沒有奏效它繼續顯示我已經嘗試123456789.12

注意:我正在填充數據庫中的datagridview,我需要動態更改單元格格式

+1

第二個應該是「「#,###。00」',但任何一個只會工作在有數字的列上。文本數據不會被格式化,您應該將其分配給數字列,以便它不嘗試將日期格式化爲該格式 – Plutonix

回答

0

如果您的列類型是數字類型(例如do不管在編輯模式下,格式化都可以完成工作。 如果列類型爲字符串類型是默認的,你768,16處理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