在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
...全文
533 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
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)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧