0

我需要从所选项目的数据源中检索“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"
4

3 回答 3

2

执行以下操作

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

If DMV IsNot Nothing Then
    SelectedMainCat = DMV.Item("id")
End If
于 2013-07-12T06:44:24.623 回答
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
于 2013-07-12T06:20:21.107 回答
0

尝试直接投射价值:

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

在这里看到它。它可能会有所帮助

于 2013-07-12T04:47:03.223 回答