• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

在ComboBox中显示数据库的内容???急!

AliceLo 2003-06-29 09:33:34
我是这样写了一个读取数据库的代码,想在名为DataItem的ComboBox中显示表中所有记录的ID列的内容作为这个ComboBox的Item,请问在????????????处应如何写?还是要对上下文作什么样的修改?请指点!

Private Sub FormDelete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



Const StrConn As String = "Persist Security Info=True;" & _
"User ID=sa;Password=sa;" & _
"Initial Catalog=TESTDATA;Data Source=INTERSUNCO;" & _
"Workstation ID=INTERSUNCO;"


Dim ObjConn As New Data.SqlClient.SqlConnection()
ObjConn.ConnectionString = StrConn


ObjConn.Open()


Dim ObjCmd As New Data.SqlClient.SqlCommand()
ObjCmd.CommandText = "Select * From TESTTABLE1"
ObjCmd.Connection = ObjConn


ObjCmd.ExecuteNonQuery()

Me.DataItem.Items.AddRange(New Object() {"???????????????"})


ObjConn.Close()


End Sub
...全文
421 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sue 2003-07-13
用数据绑定
回复
yohomonkey 2003-07-09
晕!类直接工程中建个类,把代码拷入,用给你的方法调用即可。
回复
snow66 2003-07-01
sorry,写错了,应该是:

with dataitem
.beginupdate
while odr.read
.items.add(odr.item(0))
end while
.endupdate
.selectedindex=0
end with
回复
LongBow007 2003-07-01
up
回复
AliceLo 2003-06-30
dim oDR as new sqldatareader=ObjCmd.executereader
with dataitem.items
.beginupdate
while odr.read
.add odr.item(0)
end while
.endupdate
end with

上面的不行呀,代码下面显示下划线,应该是不对吧!
回复
snow66 2003-06-30
有两种方法可以:
1.将数据填充到dataset中,然后设置combobox的datasource和displayvalue属性分别为该dataset和列名;
2.在你的基础上,定义一个datareader,取得id列的内容,然后利用add方法添加到combobox。下面是代码:

dim oDR as new sqldatareader=ObjCmd.executereader
with dataitem.items
.beginupdate
while odr.read
.add odr.item(0)
end while
.endupdate
end with
回复
AliceLo 2003-06-29
太长了,时间不多了,我没时间慢慢把它学懂,很急的,请问有简单些的吗?
回复
yohomonkey 2003-06-29
给你一个类。
Public Class ComboBoxClass
Public Structure ComboBoxDataSource
Private m_Value, m_Text As String
Public Sub New(ByVal longName As String, ByVal shortName As String)
Me.m_Value = shortName
Me.m_Text = longName
End Sub
Public ReadOnly Property ComboBoxValue() As String
Get
Return m_Value
End Get
End Property
Public ReadOnly Property ComboBoxText() As String
Get
Return m_Text
End Get
End Property
End Structure
Private Sub FillComboBoxDataSource(ByVal DS As DataSet, ByRef ComDataStruct As ArrayList, ByVal TextField As String, ByVal ValueField As String)
Dim dr As DataRow
Dim DataList As New ArrayList()
Dim drdata As ComboBoxDataSource
If DS.Tables(0).Rows.Count > 0 Then
Dim i As Integer
For i = 0 To DS.Tables(0).Rows.Count - 1
dr = DS.Tables(0).Rows(i)
drdata = New ComboBoxDataSource(dr(TextField).ToString, dr(ValueField).ToString)
DataList.Add(drdata)
Next
ComDataStruct = DataList
End If
End Sub
Public Sub BoundComboBox(ByRef ComboBox As ComboBox, ByVal DS As DataSet, ByVal TextField As String, ByVal ValueField As String)
Dim ComData As New ArrayList()
Dim ErrorClass As New Truck_WEBError.ErrorClass.DataSourceErrorClass()
If ErrorClass.DataSourceIsNotNULL(DS) = True Then
FillComboBoxDataSource(DS, ComData, TextField, ValueField)
ComboBox.DataSource = ComData
ComboBox.DisplayMember = "ComboBoxText"
ComboBox.ValueMember = "ComboBoxValue"
End If
End Sub
Public Function GetComboBoxSelectText(ByVal combobox As ComboBox) As String
Dim o As ComboBoxDataSource = combobox.SelectedItem
GetComboBoxSelectText = o.ComboBoxText
Return GetComboBoxSelectText
End Function
Public Function GetComboBoxSelectValue(ByVal combobox As ComboBox) As String
Dim o As ComboBoxDataSource = combobox.SelectedItem
GetComboBoxSelectValue = o.ComboBoxValue
Return GetComboBoxSelectValue
End Function
Public Function GetComboBoxItemTextByIndex(ByVal combobox As ComboBox, ByVal Index As Integer) As String
Dim o As ComboBoxDataSource = combobox.Items.Item(Index)
GetComboBoxItemTextByIndex = o.ComboBoxText
Return GetComboBoxItemTextByIndex
End Function
Public Function GetComboBoxItemValueByIndex(ByVal combobox As ComboBox, ByVal Index As Integer) As String
Dim o As ComboBoxDataSource = combobox.Items.Item(Index)
GetComboBoxItemValueByIndex = o.ComboBoxValue
Return GetComboBoxItemValueByIndex
End Function
Public Sub SelectedComboBoxitemByValue(ByRef ComboBox As ComboBox, ByVal Value As String)
Dim i As Integer
For i = 0 To ComboBox.Items.Count - 1
If GetComboBoxItemValueByIndex(ComboBox, i) = Value Then
ComboBox.SelectedIndex = i
End If
Next
End Sub
Public Sub SelectedComboBoxitemByText(ByRef ComboBox As ComboBox, ByVal Value As String)
Dim i As Integer
For i = 0 To ComboBox.Items.Count - 1
If GetComboBoxItemTextByIndex(ComboBox, i) = Value Then
ComboBox.SelectedIndex = i
End If
Next
End Sub
End Class

2.调用:
ComboBoxClass.BoundComboBox(ComboBoxControl, DataSet, TextFiled,ValueFiled)
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2003-06-29 09:33
社区公告
暂无公告