2012-10-19 65 views
0

使用VB.Net如何設置爲零,而不是空

我想打一個datagird單元格的值爲零,而不是空

例如

Datagridview1

Id value1 value2 value3 

01 null null 0 
02 0 null 10 
03 100 null 100 
... 

預期輸出

Id value1 value2 value3 

01 0 0 0 
02 0 0 10 
03 100 0 100 
... 

我有,而不是由任何其它方法檢查所述小區中的一個超過80列和100行,是可用的

試過代碼

Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) 
     If e.ColumnIndex = 0 AndAlso e.Value IsNot Nothing Then 
      If CInt(e.Value) = 0 Then 
       e.Value = "" 
      End If 
     End If 
    End Sub 

上面的代碼不能正常工作,當我添加細節在數據庫的datagridview中,也顯示空單元格。

如何使零而不是null。

任何其他方法或建議需要VB.Net代碼幫助

回答

6
  • 右鍵單擊DataGridView的,然後單擊屬性。
  • 在屬性窗口中,找到DefaultCellStyle
  • 單擊選項卡
  • 在出現的對話框中,指定NullValue屬性

enter image description here

您還可以設置它的代碼

dataGridView1.DefaultCellStyle.NullValue = 0;

獲取或設置System.Windows.Forms.DataGridView單元格顯示值對應的 單元格值爲System.DBNull.Value或null。

相關問題