代码:
Private m_log_dataTable As System.Data.DataTable = Nothing
Private m_freq As String = Nothing
Private m_r As Single = Nothing
Private m_l As Single = Nothing
Private m_c As Single = Nothing
Private m_rp As Single = Nothing
Private m_rs As Single = Nothing
Private m_z As Single = Nothing
Private m_esr As Single = Nothing
Private m_dcr As Single = Nothing
Private m_q As Single = Nothing
Private m_d As Single = Nothing
...
Private Sub LOG()
Try
m_freq = Nothing
m_r = Nothing
m_l = Nothing
m_c = Nothing
m_rp = Nothing
m_rs = Nothing
m_z = Nothing
m_esr = Nothing
m_dcr = Nothing
m_q = Nothing
m_d = Nothing
m_value = Nothing
m_unit = Nothing
m_log_dataTable.Rows.Add(DateTime.Now, getDUT(), getMode(), m_freq, m_r, m_l, m_c, m_r, m_rs, m_z, m_esr, m_dcr, m_q, m_d)'Line1
m_log_dataTable.Rows.Add(DateTime.Now, getDUT(), getMode(), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)'Line2
Catch ex As Exception
MsgBox("Exception when logging:" + ex.Message)
End Try
End Sub
为什么line1(在上面的代码中)将0写入数据表而不是Nothing? 我该怎么办?
由于
答案 0 :(得分:3)
Nothing
等于C#中的default(T)
(而不是null
)。值类型不能是null
因此在db中它们以not null
约束的列表示。
您确实应该为您的字段使用Nullable(Of Single)
(与Single?
相同)。
但当然这还不够。您应该修改数据库中的列,以便它们具有null
约束而不是not null
,System.Data.DataTable
应该以相同的方式配置。
答案 1 :(得分:2)
您需要使用Nullable type。将您的所有single
类型更改为single?
,然后重试。
Private m_r As Single? = Nothing
Private m_l As Single? = Nothing
// etc
正如Gabor所评论的那样,您需要访问Value
属性或Nullable(Of T)
可用的其他方法之一。
m_r.Value ' Access the underlying value
m_r.GetValueOrDefault() ' Underlying value or, if none, default for the underlying type
m_r.GetValueOrDefault(3) ' Underlying value or, if none, some default value you decide
答案 2 :(得分:0)
Coz Single的默认值为0.您可以使用字符串列,也可以定义目标datagridview的列。
DataGridViewCellStyle1.Format = "N"
Column1.DefaultCellStyle = DataGridViewCellStyle1