我遇到这个小问题,当设置了datasource属性并且我不知道如何去处理它时,无法修改Items集合。我在VB.NET工作
在这里,我在大学组合框中的MSAccess DB中填写了大学名称。在大学Combobox的SelectedChange活动中,各个学院将在另一个名为Faculty Combo box的Combobox中填充。我正在使用一个类定义和从类外的不同方法查询来执行此操作。
因此,当我尝试从University Combobox中选择任何大学时,会抛出“当设置数据源属性时无法修改项目集合”的错误。但是我无法弄清楚,看着它时一切似乎都没问题,但我知道这些内容有些不对。
这是我的代码:
类定义代码:
' Database connection
Public DBcon As New OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=DATABASE.mdb")
'Get DB Command ready
Public DBCmd As OleDbCommand
'Database data
Public DBDA As OleDbDataAdapter
Public DBDT As DataTable
'Parameter Query
Public Parameter As New List(Of OleDbParameter)
'Query Stats
Public Record As Integer
Public Exception As String
Public Sub ExecQuery(Query As String)
'Reseting the Query Stats
Record = 0
Exception = ""
Try
'Open a Connetion
DBCon.Open()
'Creating Database command
DBCmd = New OleDbCommand(Query, DBCon)
'Loading Params into Database Command
Parameter.ForEach(Sub(x) DBCmd.Parameters.Add(x))
'Clear the List of Params
Parameter.Clear()
'Execute Command and Fill the DataTable
DBDT = New DataTable
DBDA = New OleDbDataAdapter(DBCmd)
Record = DBDA.Fill(DBDT)
Catch ex As Exception
Exception = ex.Message
End Try
'Close Connection
If DBCon.State = ConnectionState.Open Then DBCon.Close()
填充大学组合框的代码:
Try
'Query string
Dim myQuery As String
myQuery = "SELECT * FROM Universities ORDER BY University_Name ASC"
'Run query
Access_DB.ExecQuery(myQuery)
' In case of Errors Report
If NoErrors(True) = False Then Exit Sub
'Fill the Combo box
'Clear Combobox
University_Choice.University_NameComboBox.Items.Clear()
'Populate the combo box
With University_Choice.University_NameComboBox
.DisplayMember = "University_Name"
.ValueMember = "University_ID"
.DataSource = Access_DB.DBDT
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
'Display the first item found
If Access_DB.Record > 0 Then University_Choice.University_NameComboBox.SelectedIndex = 0
Catch ex As Exception
Access_DB.Exception = ex.Message
End Try
填充学院组合框的代码:
Try
'Query string
Dim QueryFaculty As String
QueryFaculty = "SELECT * FROM University_Faculties " & _
"WHERE University_ID = @UniversityID"
Access_DB.ParamAdd("@UniversityID", "%" & _
University_Choice.University_NameComboBox.SelectedValue.ToString & "%")
'Run query
Access_DB.ExecQuery(QueryFaculty)
'In case of errors report
If NoErrors(True) = False Then Exit Sub
'Clear
University_Choice.cboFaculty.Items.Clear()
'Populate the combo box
With University_Choice.cboFaculty
.DisplayMember = "Faculty_Name"
.ValueMember = "University_Faculties_ID"
.DataSource = AccessDB.DBDT
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
'Display the first item found
If Access_DB.Record > 0 Then University_Choice.cboFaculty.SelectedIndex = 0
Catch ex As Exception
Access_DB.Exception = ex.Message
End Try
如果您需要任何进一步的信息,请告诉我。
答案 0 :(得分:0)
AccessDB
应为Access_DB
。
With University_Choice.cboFaculty
.DisplayMember = "Faculty_Name"
.ValueMember = "University_Faculties_ID"
.DataSource = Access_DB.DBDT 'Typo here
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With