ListBox VB.NET的ValueMember

时间:2012-05-04 23:12:14

标签: vb.net

美好的一天,

我想显示我的“Subscribers”表中的电话号码,其中包含与之关联的相同值(字符串)。例如,我想显示这些数字:

+12345678 
+87654321

这是我的代码:

Dim conStr As String = ConfigurationManager.ConnectionStrings("dbConnectionString").ConnectionString
Dim adapter As New SqlDataAdapter("Select * from tblSubscribers", conStr)
Dim dataset As DataSet = New DataSet

adapter.Fill(dataset)

ListBoxSubscribers.DataSource = dataset.Tables(0)
ListBoxSubscribers.DisplayMember = "Phone"
ListBoxSubscribers.ValueMember = "Phone"

我正在尝试遍历列表框的每个值。问题是我得到P,h,o,n,e而不是实际数字。这是我的迭代程序:

        For Each item As String In ListBoxSubscribers.ValueMember
            Dim PhoneNumber As String = item.ToString
            TextBoxPhoneNumberDisplay.Text = PhoneNumber
            SendSMS(PhoneNumber, msg)
            System.Threading.Thread.Sleep(2000)
        Next

感谢。

2 个答案:

答案 0 :(得分:0)

valuemember必须是主键,顺便说一下你不遍历整个valueMembers,它只是在该控件中设置的“字符串”,尝试将displayMember和ValueMember设置为数据集中的列,如:

listboxsubscribers.displayMember = dataset.Tables(0).columns(index)

答案 1 :(得分:0)

您的ListBoxSubscribers控件使用DataTable作为其DataSource,这意味着列表中的项目是DataRowView项目,而不是字符串。

您还遍历Items集合,而不是ValueMember属性。

快速修复:

For Each item As DataRowView In ListBoxSubscribers.Items
  Dim PhoneNumber As String = item("Phone").ToString
  '\\ etc
Next