如何解决从类型"dbnull"到类型"字符串"的转换无效 VB .NET



你好,我的代码中有以下错误

conversion from type 'DBNull' to type 'string' is not valid

请帮助。代码:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value
txt_DATE.Text = DataGridView1.CurrentRow.Cells(1).Value
txt_FNAME.Text = DataGridView1.CurrentRow.Cells(2).Value
txt_LNAME.Text = DataGridView1.CurrentRow.Cells(3).Value
txt_AGE.Text = DataGridView1.CurrentRow.Cells(4).Value
txt_SEX.Text = DataGridView1.CurrentRow.Cells(5).Value
txt_REGNO.Text = DataGridView1.CurrentRow.Cells(6).Value
txt_ROOMNO.Text = DataGridView1.CurrentRow.Cells(7).Value
txt_HEIGHT.Text = DataGridView1.CurrentRow.Cells(8).Value
txt_WEIGHT.Text = DataGridView1.CurrentRow.Cells(9).Value
txt_CELLNO.Text = DataGridView1.CurrentRow.Cells(10).Value
txt_ADDRESS.Text = DataGridView1.CurrentRow.Cells(11).Value
txt_TELLNO.Text = DataGridView1.CurrentRow.Cells(12).Value
txt_FAXNO.Text = DataGridView1.CurrentRow.Cells(13).Value
txt_EMAIL.Text = DataGridView1.CurrentRow.Cells(14).Value
txt_REFERMD.Text = DataGridView1.CurrentRow.Cells(15).Value
txt_MD.Text = DataGridView1.CurrentRow.Cells(16).Value
txt_ANES.Text = DataGridView1.CurrentRow.Cells(17).Value
txt_PROC.Text = DataGridView1.CurrentRow.Cells(18).Value
txt_INDICATION.Text = DataGridView1.CurrentRow.Cells(19).Value
txt_XRAY.Text = DataGridView1.CurrentRow.Cells(20).Value
txt_TARGET.Text = DataGridView1.CurrentRow.Cells(21).Value
txt_PREP1.Text = DataGridView1.CurrentRow.Cells(22).Value
txt_PREP2.Text = DataGridView1.CurrentRow.Cells(23).Value
txt_PREP3.Text = DataGridView1.CurrentRow.Cells(24).Value
txt_QUALITY.Text = DataGridView1.CurrentRow.Cells(25).Value
txt_SEGMENT.Text = DataGridView1.CurrentRow.Cells(26).Value
txt_BBP.Text = DataGridView1.CurrentRow.Cells(27).Value
txt_PREMED.Text = DataGridView1.CurrentRow.Cells(28).Value
txt_SCOPE.Text = DataGridView1.CurrentRow.Cells(29).Value
txt_PROCTIME.Text = DataGridView1.CurrentRow.Cells(30).Value
txt_FINDINGS.Text = DataGridView1.CurrentRow.Cells(31).Value
txt_BIOPSY.Text = DataGridView1.CurrentRow.Cells(32).Value
txt_LOC.Text = DataGridView1.CurrentRow.Cells(33).Value
txt_CYTOLOGY.Text = DataGridView1.CurrentRow.Cells(34).Value
txt_SIZE.Text = DataGridView1.CurrentRow.Cells(35).Value
txt_ENDODESC.Text = DataGridView1.CurrentRow.Cells(36).Value
txt_THERAPEUTIC.Text = DataGridView1.CurrentRow.Cells(37).Value
txt_COMPLICATIONS.Text = DataGridView1.CurrentRow.Cells(38).Value
txt_DIAGNOSIS.Text = DataGridView1.CurrentRow.Cells(39).Value
txt_OUTCOME.Text = DataGridView1.CurrentRow.Cells(40).Value
txt_LATECOMP.Text = DataGridView1.CurrentRow.Cells(41).Value
txt_HISTOPATH.Text = DataGridView1.CurrentRow.Cells(42).Value
txt_ENDOSPCOPIST.Text = DataGridView1.CurrentRow.Cells(43).Value
End Sub

尝试CheckDBNull函数。:)

ex) txt_PREP1.Text = CheckDBNull(DataGridView1.CurrentRow.Cells(22).Value)
Public Function CheckDBNull(ByVal oData As Object) As String
Try
Dim sResult As String
If TypeOf oData Is DBNull Then
sResult = ""
Else
sResult = Trim(CType(oData, String))
End If
Return sResult
Catch ex As Exception
Return ""
End Try
End Function

在代码中添加ToString。

txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()

或在

下放置if条件检查值是否为空或不像
IF not DataGridView1.CurrentRow.Cells(0).Value is dbnull Then
txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value
EndIF

但这是非常基础的。

相关内容

  • 没有找到相关文章

最新更新