无法将类型 'System.Int32' 的对象强制转换为类型 'System.Data.DataRowView'



我需要从所选项目的数据源中检索"id",但它总是在标题中抛出相同的错误。这是我的代码

        Dim DMV As DataRowView = LbMCat.SelectedValue 'Here occurs the error
    SelectedMainCat = DMV.Item("id") 
    'Filling the SUB Categories part / same code used to fill Main categories
    Dim DataAdapterCats As New MySqlDataAdapter("SELECT * From catS where maincat = '" & SelectedMainCat & "';", MySqlConnection)
    Dim dsSc As New DataSet
    DataAdapterCats.Fill(dsSc, "SubCategories")
    Dim SDataTable As DataTable = dsSc.Tables(0)
    LbSCat.DataSource = SDataTable
    LbSCat.DisplayMember = "title"
    LbSCat.ValueMember = "id"

按以下操作

Dim DMV As DataRowView = TryCast(LbMCat.SelectedItem, DataRowView)
If DMV IsNot Nothing Then
    SelectedMainCat = DMV.Item("id")
End If

如果您检查所选值是否不是整数,该怎么办??

If Not TypeOf (LbMCat.SelectedValue) Is Integer Then
    Dim DMV As DataRowView = LbMCat.SelectedValue 'Here occurs the error
    SelectedMainCat = DMV.Item("id")     
End If

尝试直接投射值:

(DirectCast(LbmCat.SelectedItem,DataRowView)("ID").ToString())

请在此处查看。它可能有助于

相关内容

  • 没有找到相关文章

最新更新