在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
...全文
577 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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)
html,css,按钮 精选按钮样式设计美学与交互体验全解析 在数字产品的交互设计,按钮作为连接用户与功能的关键桥梁,其样式设计直接影响用户体验与界面质感。本页面基于buttons.css样式库,精心呈现了一系列兼具美学价值与实用功能的按钮设计方案,为开发者与设计师提供直观的视觉参考与代码借鉴。 按钮设计看似简单,实则融合了视觉心理学、交互逻辑与品牌调性的多重考量。从极简的单色按钮到富有层次感的立体按钮,从静态的基础样式到带有hover动效的交互反馈,每一款设计都兼顾了视觉吸引力与操作清晰度。例如页面展示的药丸反色按钮,通过圆润的边角处理(button-pill类)与高对比度的色彩反转(button-inverse类),既强化了视觉焦点,又在移动端场景提升了触控识别度,尤其适合作为主要操作按钮使用。 页面采用简洁明快的布局逻辑,浅色背景与高对比度按钮形成鲜明视觉层次,每个按钮示例均搭配详细说明,清晰标注样式类名与适用场景。这种设计不仅便于开发者快速定位所需样式,更能帮助设计初学者理解"样式类组合"的设计思路——通过基础类与修饰类的灵活搭配,可衍生出海量符合不同场景需求的按钮样式,大幅提升开发效率。 无论是电商平台的购买按钮、社交应用的交互按钮,还是工具类产品的功能按钮,本页面展示的设计方案均遵循"一致性与差异性平衡"的原则:基础样式保持统一的视觉语言,确保界面整体性;特殊场景按钮通过色彩、形状或动效的微调,实现功能区分与重点突出。此外,所有按钮样式均基于标准化CSS类实现,便于开发者直接复用或根据品牌调性进行二次定制,兼顾了设计美感与开发实用性。 对于追求高效开发的团队而言,这样的样式库不仅能减少重复劳动,更能保障产品界面的设计一致性;对于设计师而言,这些经过实践验证的按钮样式可作为创意起点,在此基础上探索更具个性的设计表达。浏览本页面时,建议结合实际产品场

16,722

社区成员

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

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