2009-12-11 46 views
0

我需要在DataGridView中顯示十進制值DataGridView十進制值格式化:猶太方式

  if (dgvClients.SelectedRows.Count != 0) 
     { 
      DataGridViewRow row = dgvClients.SelectedRows[0]; 

      row.Cells["NetAccountValue"].Value = info.DecimalValue; 
     } 

我需要在十進制分隔符後將值格式化爲給定的數字。

問題是單元格Value屬性在我的情況下存儲對十進制的引用。顯示decimal.ToString()時,默認decimal.ToString()生成帶有大量數字的無格式字符串 。

一種方法是創建字符串並使用String.FormatString()來實現所需的格式 並將其提供給Value而不是decimal。

還有其他方法嗎?

回答

1

使用代碼您可以設置DataGridView.DefaultCellStyle property

您還可以通過做到這一點的設計師

  1. 右鍵單擊您的DGV和 選擇編輯列。出現編輯 列對話框。
  2. 在對話框的左側 中選擇您的列,然後單擊 DefaultCellStyle屬性字段爲 調出CellStyle Builder。
  3. 在「生成器」對話框中,單擊格式 ,它將顯示格式字符串 對話框。
  4. 在此對話框中進行選擇 (選擇數字類型,允許指定顯示小數位數)並保存您的更改。
0

我發現這個解決方案很簡單,我已經搜索了兩天,最終找到了它。 -your db列類型必須是Money或數字,雙精度,單精度。 -update datagridview event「CellFormatting」as below code。

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (this.dataGridView2.Columns[e.ColumnIndex].Name == "Aidat") 
     { 
      string deger=(string)e.Value; 
      deger = String.Format("{0:0.00}", deger); 
     } 
    }