如何向已绑定到数据库的组合框添加项目 [英] how to add an item to a combobox which will already bound to database

查看:152
本文介绍了如何向已绑定到数据库的组合框添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 在我的应用程序中,组合框绑定到数据库.我想要一个项目
在运行时,如果我尝试将其添加到该组合框,则会显示错误消息
项目集合无法修改.如果有人知道如何操作.
帮助我提供一些代码示例

在此先感谢.

解决方案



当您将数据源设置为ComboBox 并尝试更改ComboBox 项时,会发生此错误.

您应该更改数据源,而不是更改ComboBox 项.

以下是可以帮助您的解决方案:
http://shawpnendu.blogspot.in/2010/06/error-items- collection-cannot-be.html [ ^ ]


非常简单

你什么也没做

从数据库绑定组合框时,要将新项目添加到该组合框中,必须更改数据源.

让我们看一个使它变得更好的例子.


假设cb1是您的组合框,并且字段名称是Database中的District.

当您从数据库绑定它时,它会显示地区名称,现在您想添加一些运行时值.

在数据库中,您可以将数据源设置为

 cb1.DataSource = ds.Tables [ 0 ]或cb1.DataSource = dt; // 其中dt = ds.Tables [0]; 
cb1.DisplayMember = " ; 



使用下面的代码:

 DataTable Dt1 =  DataTable();
Dt1.Columns.Add(" );
Dt1.Rows.Add(" );
Dt1.Rows.Add(" ); 



现在

 dt.Merge(Dt1); 



再次设置为

 cb1.DataSource = dt;
cb1.DisplayMember = " ; 



这将在ComBoBox中添加两个名为德里和哈里亚纳邦的新区.

这将解决您的问题


清除combox,首先添加新的


hi in my application combobox is bound to the database.i want to an item
at runtime to that combobox if i try to add it will display an error message
item collection cannot be modified.if anyone have any idea how to do it.
help me with some code examples

thanks in advance.

解决方案

Hi,

This error occurs when you have set data source to the ComboBox and try to change ComboBox items.

You should change the data source instead of changing ComboBox items.

Here is a solution that might help you:
http://shawpnendu.blogspot.in/2010/06/error-items-collection-cannot-be.html[^]


Its Pretty Simple

You have to do nothing

when you bound your combobox from the database then for adding a new item into that combobox you must change the datasource.

Let''s see an example to make it better.


Suppose cb1 is your combobox and the field name is District in Database.

when you bound it from databse it shows name of district, Now you want to add some run time values.

from Database you set the datasource like

cb1.DataSource=ds.Tables[0]  or   cb1.DataSource=dt;    // where dt=ds.Tables[0];
cb1.DisplayMember="District";



Go with the code as below:

DataTable Dt1=new DataTable();
Dt1.Columns.Add("District");
Dt1.Rows.Add("Delhi");
Dt1.Rows.Add("Haryana");



Now

dt.Merge(Dt1);



again set like

cb1.DataSource=dt;    
cb1.DisplayMember="District";



This will add two new Districts named Delhi and Haryana in ComBoBox

This will solve your Problems


Clear the combox first add new


这篇关于如何向已绑定到数据库的组合框添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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