2014-04-04 215 views
0

我有一個datagridview列,當表單加載是一個十進制值時,該值顯示爲0.00。當我點擊特定的單元格時,我想清除此列單元格的值,當我離開單元格時,值(0.00)應該出現在單元格中。爲了更好的理解,我附上了快照。如何從單元格單擊中的datagridview單元格中刪除值?

enter image description here

我想這在特定的細胞點擊。十進制值可能嗎?如何將單元格設置爲空?我在單元格單擊上做了一些代碼,但沒有結果。

private void dgvForecast_CellClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.ColumnIndex == 3) 
      { 
       var cell = dgvForecast.Rows[e.RowIndex].Cells[e.ColumnIndex].Style as DataGridViewCellStyle; 
       decimal val = Convert.ToDecimal(dgvForecast.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()); 
       cell.Format = ""; 
       dgvForecast.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val.ToString("#"); 
       cell.ApplyStyle(cell); 
      } 
     } 

回答

1

刪除單元格的值,你可以使用

if (e.ColumnIndex == 3) 
     { 
      dataGridVirtuals[e.ColumnIndex, e.RowIndex].Value = ""; 
     } 

用於把一個值成失去焦點,您可以使用CellLeave事件 如

if(e.ColumnIndex==3) 
{ 
dataGridView[e.ColumnIndex,e.RowIndex].Value=[yourvalue]; 

} 
+0

我已經使用的電池這仍然相同0.00仍然。 –

+0

嘗試解析字段爲DataGridViewTextBoxCell,因爲我試圖在我的機器上工作..您的單元格是正確的文本框? – gulshanm01

+0

是的我的單元格是textboxcell,但與數據源綁定的值是雙數據類型。 –