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

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

问题描述

Using mySqlConn As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Dell\Desktop\Assignment\Assignment\ace.mdf;Integrated Security=True")
           Try
               mySqlConn.Open()
               query = "select TOTAL from ROOM Where ROOMID = '" & ddlReRt.SelectedValue & "'"
               cmd = New SqlCommand(query, mySqlConn)
               reader = cmd.ExecuteReader
               da.SelectCommand = cmd

               If reader.Read() Then
                   Dim id As Integer = reader("TOTAL")
                   For i As Integer = 1 To id

                       ddlReRt.Items.Add(i)
                       Console.Write(i)
                   Next
                   ' Console.Write(id)
                   reader.Close()
               End If


               mySqlConn.Close()

           Catch ex As Exception
               MessageBox.Show(ex.Message)
           Finally
               mySqlConn.Dispose()
           End Try
       End Using





目前我遇到了向Combobox添加Numbers的问题。我想要的结果是,当我从数据库中检索整数9时,将循环并添加到组合框中,然后组合框将显示1,2,3,4,5,6,7,8,9。 />


我尝试了什么:



当我使用控制台写入时,它可以打印出1,2,3,4,5,6,7,8,9。但我不知道如何将数字添加到Combobox中进行显示。



Currently i have the problem with add Numbers to the Combobox. The result i wants is, when i retrieve a Integer 9 from the database, will loop it and add into the combobox, then the combobox will display 1,2,3,4,5,6,7,8,9.

What I have tried:

When i using console write, it can print out 1,2,3,4,5,6,7,8,9. But i have no idea how to add the numbers into the Combobox to display.

推荐答案

代码中的其他地方你已经设置了ComboBox的DataSource-Property,看起来类似这样的事情:

Somewhere else in your code you have set the DataSource-Property of the ComboBox, looking something like this:
ddlReRt.DataSource = something





你可以将ComboBox的DataSource设置为某种类型的集合以显示项目使用 ddlReRt.Items.Add(i)添加项目但不是结合这两种方式。 *



因此,您需要在此处添加项目之前删除绑定的DataSource(只需通过设置 DataSource = Nothing )或首先将项添加到集合中,然后将该集合设置为ComboBox的新DataSource。 (或者,理论上,将这些项目添加到现有的集合中,这是当前的数据源,但这很可能不是您想要的。)



* 编辑:任何具有DataSource属性的控件都是这种情况(例如DataGridView)



You can either set a ComboBox' DataSource to a collection of some sort to display the items or add items with ddlReRt.Items.Add(i) but not combine these two ways. *

So either you would have to remove the bound DataSource before you add the items here (simply by setting DataSource = Nothing) or add the items to a collection first here and then set that collection as the ComboBox' new DataSource. (Or, theoretically, add the items here to the existing collection which is the current DataSource but that's most probably not what you want.)

* This is the case for any control that has a DataSource-property (e.g. DataGridView)


由于选择了错误的组合框名称,问题就解决了。对不起我的粗心
The problem is solve due to wrong combobox name selected. Sorry for my careless


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

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