设置数据源属性时,无法修改项目集合 [英] Items collection cannot be modified when the datasource property is set

查看:75
本文介绍了设置数据源属性时,无法修改项目集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,在设置datasource属性并且我不知道如何解决此问题时,无法修改项目集合.

Hi am having this little problem of Items collection cannot be modified when the datasource property is set and i don't know how to go about it, Please help me if possible.

在这里,我从大学组合框中的MSAccess数据库中填充了大学的名称.但是我想做的是

Here, i have populated names of Universities from my MSAccess DB in the University Combobox. But what i want to do is,

在大学组合框的SelectedChange事件上,然后将各个学院填充到另一个名为组合框"的组合框中.我正在使用一个类定义并从外部的不同方法查询来执行此操作 班级.

On the SelectedChange Event of the University Combobox Then the respective Faculties will be populated in another Combobox called Faculty Combo box. I am performing this operation using one Class definition and Querying from different methods outside the class.

因此,设置数据源属性时不能修改项目集合"的错误.当我尝试从大学组合"框中选择任何大学时,都会引发该错误.但是我可以弄清楚,看着它,一切似乎都还可以,但我知道一些 行内是错误的.

So the error of "Items collection cannot be modified when the datasource property is set" is thrown when i try to select any University from the University Combo box. But i can figure it out, Everything seems OK while looking at it but i know something is wrong within the lines.

这是我的代码:

类定义代码:

    ' 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()

填充大学组合框的代码:

Code for Populating the University Combo box:

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

填充教员组合框的代码:

Code for Populating the Faculties Combo box:

 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

如果您需要任何其他信息,请告诉我.谢谢

Please if you need any further Info please let me Know. Thanks

推荐答案

尝试删除'University_Choice.cboFaculty.Items.Clear()',因为您使用的是数据"绑定",方法是将数据表分配给 数据源.在这种情况下,应更改数据表或其行,而不是影响组合框的项目.在你的代码中 由于重新创建了关联的数据表,因此不需要清除.

Try removing ‘University_Choice.cboFaculty.Items.Clear()’, since you use "data binding" by assigning a data table to DataSource. In this case, instead of affecting the items of the combobox, you should change the data table or its rows. In your code Clear is not needed since you recreate the associated data table.

如果要清除组合框并取消数据绑定,还可以执行" University_Choice.cboFaculty.DataSource = Nothing".

If you want to clear the combobox and cancel data binding, you can also execute ‘University_Choice.cboFaculty.DataSource = Nothing’.


这篇关于设置数据源属性时,无法修改项目集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆