我希望在插入直接进入Combobox的值后更新Combobox。可能我需要进行查询,我已经知道了。这是我目前获得的代码
Private Sub UpdateComboBox()
SQLCon = New SqlConnection
SQLCon.ConnectionString = "...."
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
SQLDataReader = SqlCmd.ExecuteReader
ComboBox1.DataSource = ComboBox1.Items.Add(Query)
SQLCon.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
End Sub
我想在插入内容后立即添加此方法。
此代码的格式为
SQLCon = New SqlConnection
SQLCon.ConnectionString = "......"
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
SQLDataReader = SqlCmd.ExecuteReader
While SQLDataReader.Read
Dim fileType = SQLDataReader.GetString(0)
DataGridView1.DataSource = ComboBox1.Items.Add(fileType)
End While
SQLCon.Close()
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
答案 0 :(得分:0)
试试这个:
Private Sub UpdateComboBox()
SQLCon = New SqlConnection
SQLCon.ConnectionString = "....."
Try
SQLCon.Open()
Dim Query As String
Query = "SELECT Filetype FROM infofile GROUP BY Filetype"
SqlCmd = New SqlCommand(Query, SQLCon)
Dim adapter As New SqlDataAdapter(SqlCmd)
Dim table As New DataTable
adapter.Fill(table)
ComboBox1.DataSource = table
ComboBox1.DisplayMember = "Filetype"
SQLCon.Close()
Return table
Catch ex As SqlException
MsgBox(ex.Message)
Finally
SQLCon.Dispose()
End Try
End Sub