【问题标题】:Unable to cast object of type 'System.Int32' to type 'System.Data.DataRowView'无法将“System.Int32”类型的对象转换为“System.Data.DataRowView”类型
【发布时间】:2013-07-10 12:45:17
【问题描述】:

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

【问题讨论】:

    标签: vb.net datarowview


    【解决方案1】:

    如下操作

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

    【讨论】:

      【解决方案2】:

      如果你检查选择的值不是整数怎么办??

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

      【讨论】:

        【解决方案3】:

        尝试直接投射值:

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

        See it here。它可能会有所帮助

        【讨论】:

        • 同样的错误,无法将“System.Int32”类型的对象转换为“System.Data.DataRowView”类型。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        • 1970-01-01
        • 1970-01-01
        • 2012-01-23
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多