[英]Combo box not displaying information VB.NET
I'm working on a form with multiple combos boxed and one of them isn't working for me.我正在处理一个带有多个组合框的表单,其中一个不适合我。 The information is being pulled from one table in my database.信息是从我的数据库中的一个表中提取的。
All other ones are working to find way more complex queries and it even depends on the results on the previous one, but this one is a stand-alone.所有其他人都在努力寻找更复杂的查询方式,甚至取决于前一个的结果,但这个是独立的。
the idea is to look up the names of users in a database and display them and when selecting we use the user code (SQL):这个想法是在数据库中查找用户的名称并显示它们,选择时我们使用用户代码(SQL):
select CashierCode, Name from cashier
results = 8011 users in the database结果 = 数据库中有 8011 个用户
maybe the issue is with the number of users?也许问题出在用户数量上?
code goes as following:代码如下:
Private Sub cmbUserScanned_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbUserScanned.SelectedIndexChanged
Try
Application.DoEvents()
'cmbUserScanned.Items.Clear()
Dim strSQL As String
Dim theList As DataTable
strSQL = "select CashierCode, Name from cashier"
SQL.WriteOutSQLToLogFile(strSQL)
theList = _SQL.GetDBDataTableSimple(strSQL)
If theList Is Nothing Then
WriteOutToLogFile("Found no operator in the cashier table")
Exit Sub
End If
cmbUserScanned.DataSource = theList
cmbUserScanned.DisplayMember = "Name"
cmbUserScanned.ValueMember = "CashierCode"
Catch ex As Exception
WriteOutToLogFile("Found no operator in the cashier table : " & ex.ToString)
End Try
End Sub
You have to distinguish two things:你必须区分两件事:
Populate the drop-down part of the combobox.填充 combobox 的下拉部分。
Populate the grid, depending on a selection in the combobox.根据 combobox 中的选择填充网格。
You seem to have mixed up these two requirements.您似乎混淆了这两个要求。 Fix it by populating the combobox at the Form Load event:通过在表单加载事件中填充 combobox 来修复它:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Dim strSQL As String
Dim theList As DataTable
strSQL = "SELECT CashierCode, Name FROM cashier"
theList = _SQL.GetDBDataTableSimple(strSQL)
If theList Is Nothing Then
WriteOutToLogFile("Found no operator in the cashier table")
Exit Sub
End If
cmbUserScanned.DataSource = theList
cmbUserScanned.DisplayMember = "Name"
cmbUserScanned.ValueMember = "CashierCode"
Catch ex As Exception
WriteOutToLogFile("Found no operator in the cashier table : " & ex.ToString)
End Try
End Sub
Then when the user selects an item in the combobox, update the grid然后当用户在 combobox 中选择一个项目时,更新网格
Private Sub cmbUserScanned_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles cmbUserScanned.SelectedIndexChanged
If cmbUserScanned.SelectedValue Is Nothing Then ' No selection
myGrid.DataSource = Nothing
Exit Sub
End If
Try
Dim strSQL As String
Dim theList As DataTable
strSQL = "SELECT Data1, Data2, ... FROM cashier_related_data WHERE CashierCode = " &
CInt(cmbUserScanned.SelectedValue)
theList = _SQL.GetDBDataTableSimple(strSQL)
If theList Is Nothing Then
WriteOutToLogFile("Found no cashier related data")
Exit Sub
End If
myGrid.DataSource = theList
Catch ex As Exception
WriteOutToLogFile("Found no cashier related data : " & ex.ToString)
End Try
End Sub
I also urge you to use parametrized queries .我还敦促您使用参数化查询。 They are both: safer and more efficient.它们都是:更安全、更高效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.