You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataRow转String无效:DataTable填充ComboBox报错求助

问题:DataTable填充ComboBox时出现“从DataRow转换到String无效”错误

使用DataTable填充ComboBox时触发“conversion from type datarow to type string is invalid”错误,同款代码在其他程序运行正常,本程序中报错但ComboBox仍能正常填充显示内容,报错行已在代码中注释标注。

原代码

Using con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & VE_docPath & VE_currentProject & ".accdb;Persist Security Info=True")
    Using cmd As New OleDb.OleDbCommand("", con)
        Try
            'cmd.CommandText = "(SELECT * FROM PartDefinitions WHERE PartClass = 3)"
            cmd.CommandText = "(SELECT PartDefID, PartClass, PartNumber, PartDescription FROM PartDefinitions WHERE PartClass = 3)"
            cmd.CommandType = CommandType.Text
            cmd.Connection = con

            con.Open()
            Dim daGet As New OleDbDataAdapter()
            Dim backshells_dt As New DataTable
            Dim backshell_index As Integer = 0
            Dim Backshells_DataRow As DataRow
            daGet.SelectCommand = cmd
            daGet.Fill(backshells_dt)

            'Concatenate the part number and description columns and display that so the user does not have
            'to figure out what just the part number is
            backshells_dt.Columns.Add("concat")
            For Each item As DataRow In backshells_dt.Rows
                item("concat") = item("PartNumber") & ", " & item("PartDescription")
            Next

            'add a "None" row so we can make it the selected item
            Backshells_DataRow = backshells_dt.NewRow()
            Backshells_DataRow("PartDefId") = "0"
            Backshells_DataRow("PartClass") = "3"
            Backshells_DataRow("PartNumber") = "None"
            Backshells_DataRow("concat") = "None"
            backshells_dt.Rows.Add(Backshells_DataRow)


            If backshells_dt.Rows.Count > 0 Then
                Me.BackshellComboBox.DataSource = backshells_dt 'why am I getting datarows to string error here?
                Me.BackshellComboBox.DisplayMember = "concat" 'why am I getting datarows to string error here?
                Me.BackshellComboBox.ValueMember = "concat" 'why am I getting datarows to string error here?
                backshell_index = Me.BackshellComboBox.FindStringExact("None")
                Me.BackshellComboBox.SelectedIndex = backshell_index
            End If

        Catch SqlError As System.Data.SqlTypes.SqlTypeException
            MsgBox(SqlError.Message, MsgBoxStyle.SystemModal, "Error")

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.SystemModal, "Error")
        End Try
    End Using
End Using

问题原因分析

  1. 绑定顺序错误:先设置DataSource再指定DisplayMember/ValueMember,此时ComboBox会尝试直接渲染DataRow对象,触发类型转换错误
  2. 新增列类型未指定:concat列默认是Object类型,绑定过程中解析类型时可能出现异常
  3. 控件属性差异:当前程序中ComboBox的DropDownStyle或其他绑定相关属性与正常程序不同,导致即时解析逻辑触发错误

解决方案

方案1:调整绑定顺序(最有效)

先设置DisplayMember和ValueMember,再赋值DataSource,避免ComboBox在未明确显示字段时尝试解析DataRow:

If backshells_dt.Rows.Count > 0 Then
    ' 先指定显示和值成员
    Me.BackshellComboBox.DisplayMember = "concat"
    Me.BackshellComboBox.ValueMember = "concat"
    ' 再设置数据源
    Me.BackshellComboBox.DataSource = backshells_dt
    backshell_index = Me.BackshellComboBox.FindStringExact("None")
    Me.BackshellComboBox.SelectedIndex = backshell_index
End If

方案2:显式指定新增列的数据类型

添加concat列时明确指定为String类型,避免Object类型带来的解析问题:

' 替换原新增列的代码
backshells_dt.Columns.Add("concat", GetType(String))

方案3:检查ComboBox控件属性

  • 确认DropDownStyle设置为DropDownList(推荐)或DropDown,避免Simple模式可能引发的即时渲染问题
  • 检查控件是否残留之前的绑定配置,可在设置数据源前先清空:Me.BackshellComboBox.DataSource = Nothing

内容的提问来源于stack exchange,提问作者Perry 59

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 16:40:56