5

我在这里更新了我的问题..我正在使用一个没有电话号码的组合框。我想在变量中一个接一个地获取电话。现在正在使用下面的代码来获取组合框值。但现在仍然收到以下错误消息 System.Data.DataRowView。请帮我解决这个错误。我是 vb.net 的新手。

我的部分代码在这里..

        For i = 0 To ComboBox1.Items.Count
            Dim s As String

            s = Convert.ToString(ComboBox1.Items(i))
        Next i
4

6 回答 6

4

您正在使用从零开始的索引。

改变这个:

For i = 0 To ComboBox1.Items.Count

对此:

For i = 0 To ComboBox1.Items.Count - 1
于 2015-12-30T10:31:43.707 回答
2

您的问题可能发生在这里:

s = Convert.ToString(ComboBox1.Items(i))

这不会返回值。它返回给定索引处对象的字符串表示形式,在您的情况下显然是 type System.Data.DataRowView

您必须转换ComboBox1.Items(i)为适当的类型并访问其Value. 或者,由于它是 a DataRowView,您可以通过适当的列名访问这些值:

Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")

尽管如此,首先你绝对应该关闭并处理连接,无论事务是失败还是成功。这可以在finally块中(选项 1)或using语句(选项 2)中完成。

选项1

// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
    // ...
Catch ex As Exception
    Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
    con1.Close()
    con1.Dispose()
End Try

选项 2

// ...
Using con1 as New MySqlConnection(str)
    con1.Open()
    Try
        // ...
    Catch ex As Exception
        Lblmsg.Text = " Error in data insertion process....." + ex.Message
    Finally
        con1.Close()
    End Try
End using
于 2012-06-12T10:05:18.910 回答
2

这也有效!

        Dim stgTest = "Some Text"
        Dim blnItemMatched As Boolean = False

        '-- Loop through combobox list to see if the text matches
        Dim i As Integer = 0
        For i = 0 To Me.Items.Count - 1
            If Me.GetItemText(Me.Items(i)) = stgTest Then
                blnItemMatched = True
                Exit For
            End If
        Next i

        If blnItemMatched = False Then

            Dim stgPrompt As String = "You entered  '" & stgTypedValue & "',  which is not in the list."
            MessageBox.Show(stgPrompt, "Incorrect Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Me.Text = ""
            Me.Focus()

        End If
于 2015-06-05T20:03:58.987 回答
2

即使经过很长时间,您也可以通过以下方式实现这一目标

For Each item As Object In combx.Items

    readercollection.Add(item.ToString)

        Next
于 2015-08-03T13:27:22.733 回答
2

请试试这个

  For j As Integer = 0 To CboCompany.Items.Count - 1
              Dim obj As DataRowView = CboCompany.Items(j)
              Dim xx = obj.Row(0)


              If xx = "COMP01" Then
                   CboCompany.SelectedIndex = j
                   Exit For
              End If
  Next
于 2019-01-11T23:22:57.697 回答
1

我无法在网上完整找到这个答案,但将其拼凑在一起。在下面的代码片段中,cbox 是一个 ComboBox 控件,它初始化了 DisplayMember 和 ValueMember 属性。

    Dim itemIE As IEnumerator = cbox.Items.GetEnumerator
    itemIE.Reset()
    Dim thisItem As DataRowView
    While itemIE.MoveNext()
        thisItem = CType(itemIE.Current(), DataRowView)
        Dim valueMember As Object = thisItem.Row.ItemArray(0)
        Dim displayMember As Object = thisItem.Row.ItemArray(1)
        ' Insert code to process this element of the collection. 
    End While
于 2013-10-05T02:17:28.033 回答