如何在运行时填充DataListBox?

时间:2015-09-21 20:23:56

标签: mysql vb6

我的表单上有一个DataGrid,可以正常工作..我想摆脱DataGrid并使用DataList,我该怎么做。我用来填充DataGrid的代码如下:

Private Sub Form_Load()
cn.CursorLocation = adUseClient
cn.Open "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testitem;User=root;Password=mysql;Option=3;
End Sub"

Private Sub Command1_Click()
rs.Open "select * from item where itemname = '" & Text9.Text & "'", cn, adOpenDynamic, adLockOptimistic
Set DataList1.RowSource = rs
DataList1.DataField = itemno
Set DataGrid3.DataSource = rs
End Sub

数据网格填充得很好,但是数据主义者总是空的

我在这里缺少什么? 请帮忙。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码段(未经测试):

 Private Sub Command1_Click()
   rs.Open "select * from item where itemname = '" & Text9.Text & "'", cn, adOpenDynamic, adLockOptimistic
   Set DataGrid3.DataSource = rs     
   '//Set DataList1.RowSource = rs
   '//DataList1.DataField = itemno

   With rs
     Do While Not rs.eof
       Me.ListBox1.AddItem rs!itemno
       .MoveNext
     Loop
     .Close
   End With
 End Sub

 Private Sub Command1_Click()
   rs.Open "select * from item where itemname = '" & Text9.Text & "'", cn, adOpenDynamic, adLockOptimistic
   Set DataGrid3.DataSource = rs  
   Set DataList1.RowSource = rs  
   DataList1.ListField = "itemno"
 End Sub