2012-07-05 71 views
2

設置顯示格式,我是新來的WinForms開發,目前我保持.NET 2.0中的WinForm的DataGridView的

開發應用程序中的應用程序,我有這顯示與單位值列稱爲長格。我已經使用CellFormatting事件來格式化單元格值,否則它只是數字。

但是,當用戶開始編輯我不想要單位顯示,用戶應該被允許輸入唯一的數字。

有什麼簡單的方法可以做到嗎?要在網格上設置的事件或屬性?

enter image description here

回答

1

您應該設置單元事件DataGridView_CellFormatting

void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (e.ColumnIndex == 1) 
    { 
     int value; 
     if(e.Value != null && int.TryParse(e.Value.ToString(), out value)) 
     { 
      e.Value = value.ToString("#mm"); 
     } 
    } 
} 
+0

我已經這樣做了,這就是我如何顯示1毫米。但是當細胞進入編輯模式時,我只想顯示數字。 –

+0

或嘗試使用DataGridView1_CellValueChanged(對象發件人,DataGridViewCellEventArgs e)事件 – JohnnBlade

1

,您可以設置格式字符串中使用CellStyle Builder中設置的自定義格式#毫米

怎麼做:

  1. 右鍵點擊網格,然後點擊屬性
  2. 在屬性窗口中,單擊會彈出了編輯列對話框
  3. 按鈕選擇要格式化
  4. 在編輯欄右側的對話框中選擇DefaultCellStyle屬性的細胞
  5. 點擊DefaultCellStyle屬性,那麼CellStyleBuilder對話框將打開
  6. 在這裏,你的格式屬性,這會給你的格式字符串對話框
  7. 設置自定義屬性,以#MM,你會看到預覽底部
  8. 單擊確定...直到您回到您的網格...
1

您應該處理EditingControlShowing事件以更改當前單元格格式。

private void dataGridView1_EditingControlShowing(object sender, 
           DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (dataGridView1.CurrentCell.ColumnIndex == 1) 
     { 
      e.CellStyle.Format = "#"; 
      e.Control.Text = dataGridView1.CurrentCell.Value.ToString(); 
     } 
    } 
相關問題