vb.net - DropDownStyle(DropDownList)的ComboBox控件无法显示数据
问题描述
我有以下要求,
我有一个 ComboBox 控件(DropDownList 样式),用户必须选择一个给定的值,但不能编辑。然后我将它保存到数据库表中,它工作正常。
(dataRow("it_discount_profile") = Trim(cmbDisProfile.Text))
但是,当我尝试通过从数据库中检索它来在同一个 ComboBox 中显示相同的数据时,它不会显示。
(cmbDisProfile.Text = Trim(tempTb.Rows(0).Item("it_discount_profile")))
当我将 ComboBox 更改为“DropDown Style”时,它可以工作。但随后用户可以对其进行编辑。
我在这里遗漏了什么还是那样?任何建议将不胜感激。
我使用一个过程在运行时填充它。
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
cmbDisProfile.DataSource = tempTb
cmbDisProfile.DisplayMember = "discount_profile"
End Sub
好的。实际上,我试图将我的一个旧项目从 VB 迁移到 VB.Net。VB.Net 对我来说并不新鲜。我使用自建类来减少其他地方的代码。我附上下面的课程。
我的实际要求是从表格中填充组合框。我喜欢在运行时做。我不希望用户编辑它。如果他们想添加一个新值,他们有单独的地方(表格)。我认为我做错了。如果可能,请提供示例代码,因为我不熟悉建议的方法。
Public Function myFunctionFetchTbData(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedDataTable As DataTable = fetchedDataSet.Tables(0)
Return fetchedDataTable
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
' this sub will update a table
Public Sub MyMethodUpdateTable(ByVal sqlString As String, ByVal tbToUpdate As DataTable)
Dim SqlCmd As New SqlCommand(sqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim objCommandBuilder As New SqlClient.SqlCommandBuilder(dataAdapter)
dataAdapter.Update(tbToUpdate)
End Sub
Public Function MyMethodfindRecord(ByVal strSearckKey As String, ByVal tableName As String, ByVal strColumnName As String) As Boolean
Try
Dim searchSql As String = "SELECT * FROM " & tableName & " WHERE " & strColumnName & "='" & strSearckKey & "'"
'Dim searchString As String = txtCategoryCode.Text
' searchOwnerCmd.Parameters.Clear()
' searchOwnerCmd.Parameters.AddWithValue("a", "%" & search & "%")
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(searchSql)
If tempTb.Rows.Count = 0 Then
Return False
Else
Return True
End If
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
Public Function myFunctionFetchSearchTB(ByVal inputSqlString As String) As DataTable
Try
Dim SqlCmd As New SqlCommand(inputSqlString, conn)
Dim dataAdapter As New SqlDataAdapter(SqlCmd)
Dim fetchedDataSet As New DataSet
fetchedDataSet.Clear()
dataAdapter.Fill(fetchedDataSet)
Dim fetchedSearchTB As DataTable = fetchedDataSet.Tables(0)
Return fetchedSearchTB
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
解决方案
谢谢大家的建议。唯一可以做到的方法就是你说的方法。我想把工作代码和一些要点放在所有人的利益上。
建议的,
- ComboBox1.SelectedIndex = comboBox1.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))"
当您将数据表绑定到组合框时不起作用。如果上述“SelectedIndex”方法有效,则应在运行时将这些值添加到组合框中。将项目添加到组合框的代码如下(myClassTableActivities 是我自己定义的一个类,如上所示),
Private Sub filldisProfiles()
Dim sqlString As String = "SELECT discount_profile FROM tb_discount_profiles"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
Dim i As Integer = 0
For i = 0 To tempTb.Rows.Count - 1
cmbDisProfile.Items.Add(Trim(tempTb.Rows(i).Item("discount_profile")))
Next
End Sub
添加后我们可以使用以下代码在组合框(DropDownList)上显示数据。
Private Sub txtItCode_TextChanged(sender As Object, e As EventArgs) Handles txtItCode.TextChanged
Try
Dim sqlString As String = "SELECT * FROM tb_items where it_code='" & Trim(txtItCode.Text) & "'"
Dim tempTb As DataTable
Dim myTbClass As myClassTableActivities = New myClassTableActivities()
tempTb = myTbClass.myFunctionFetchTbData(sqlString)
If Len(txtItCode.Text) < 4 Then
cmdAdd.Enabled = False
cmdDelete.Enabled = False
cmdSave.Enabled = False
Else
If tempTb.Rows.Count > 0 Then
With tempTb
txtItName.Text = Trim(tempTb.Rows(0).Item("it_name"))
cmbDisProfile.SelectedIndex = cmbDisProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_discount_profile")))
cmbProfitProfile.SelectedIndex = cmbProfitProfile.FindStringExact(Trim(tempTb.Rows(0).Item("it_profit_profile")))
cmbItCategory.SelectedIndex = cmbItCategory.FindStringExact(Trim(tempTb.Rows(0).Item("it_category")))
cmbFinanCategory.SelectedIndex = cmbFinanCategory.FindStringExact((tempTb.Rows(0).Item("it_finance_category")))
End With
cmdAdd.Enabled = False
cmdDelete.Enabled = True
cmdSave.Enabled = True
Else
cmdAdd.Enabled = True
cmdDelete.Enabled = False
cmdSave.Enabled = False
txtItName.Text = ""
Call fillItCategories()
Call fillProProfile()
Call filldisProfiles()
Call fillFinCat()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
推荐阅读
- r - 在 groupby 之后在多列中应用不同的功能
- kettle - PDI - 更新记录表中的字段值
- debugging - Lua - Lua 库中是否有与 debug.getupvalues / debug.getupvalue 相同或相似的不同函数?
- javascript - React 可加载组件的基本渲染测试
- sitecore - 没有在 VS Ultimate 2012 的扩展和更新中安装 Sitecore Rocks 选项
- ruby-on-rails - 如何优化ruby中的重复哈希?
- java - 与 For Loop Java 混淆
- vim - 意外令牌错误附近的语法错误
- networking - 发现树莓派的 IP
- angular - C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Platforms\x64\PlatformToolsets\v140\Toolset.targets(36,5)