2013-07-12 203 views
0

我需要从所选项目的数据源中检索“id”,但它总是抛出标题中提到的相同错误。这里是我的代码无法投入'System.Int32'类型的对象来键入'System.Data.DataRowView'

 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" 

回答

2

待办事项如下

Dim DMV As DataRowView = TryCast(LbMCat.SelectedItem, DataRowView) 

If DMV IsNot Nothing Then 
    SelectedMainCat = DMV.Item("id") 
End If 
+0

我不能够感谢你:) –

0

尝试直接铸造的价值:

See it here(DirectCast(LbmCat.SelectedItem,DataRowView的)( “ID”)的ToString())。它可能有帮助

+0

同样的错误,无法投类型 'System.Int32' 的对象键入 'System.Data.DataRowView'。 –

0

如果你检查的是所选的值是不是整数?

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