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

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

问题描述

我在尝试插入新项目到组合框中时出现错误



错误:项目集合无法

  string connectionstring =MultipleActiveResultSets = True; Data Source = ECSTSRD;初始目录= PMIDB;用户ID = pnpuser;密码= pnpuser123; 
sqlConnection myconnection = new SqlConnection(connectionstring);

myconnection.Open();
string custPOsql =INSERT INTO cust_po(cust_code,po_no)VALUES(@cust_code,@po_no);
SqlCommand custPOcom = new SqlCommand(custPOsql,myconnection);
custPOcom.Parameters.AddWithValue(@ cust_code,cboCustCode.Text);
custPOcom.Parameters.AddWithValue(@ po_no,cboPO.Text);
custPOcom.ExecuteNonQuery();
MessageBox.Show(Insert Successfully,Insert);
cboPO.Items.Add(cboPO.Text);

FYI,我在表单加载中绑定PO组合框的数据源。



我在stackoverflow以及其他论坛中发现了很少类似的问题,他们提到的方法是使用 cboPO.Items.Add(cboPO.Text)将新项目添加到组合框中,但我无法将其用作显示的错误。



任何人都知道是什么问题,请帮助。 b
$ b

提前感谢。

解决方案

您的cboPO控件已与显示数据的数据源绑定



如果要在您的combobox中添加行,请使用此方法

  DataTable dt = cboPO.Datasource; 
DataRow dr = dt.NewRow();
dr [ColumnName/ Index number] = cboPO.Text;


I am getting the error while I am trying to insert a new item into combo box

Error: Items collection cannot be modified when the DataSource property is set.

string connectionstring = "MultipleActiveResultSets=True;Data Source=ECSTSRD;Initial Catalog=PMIDB;User ID=pnpuser;Password=pnpuser123";
sqlConnection myconnection = new SqlConnection(connectionstring);

myconnection.Open();
string custPOsql = "INSERT INTO cust_po (cust_code, po_no) VALUES (@cust_code, @po_no)";
       SqlCommand custPOcom = new SqlCommand(custPOsql, myconnection);
       custPOcom.Parameters.AddWithValue("@cust_code", cboCustCode.Text);
       custPOcom.Parameters.AddWithValue("@po_no", cboPO.Text);
       custPOcom.ExecuteNonQuery();
       MessageBox.Show("Insert Successfully", "Insert");
       cboPO.Items.Add(cboPO.Text);

FYI, I bind the data source of PO combobox in form load.

I have found few similar question in stackoverflow as well as others forums, the way they mention is using cboPO.Items.Add(cboPO.Text) to add new item into combo box, but I could not use it as error shown.

Do anyone know what is the problem, please help.

Thanks in advance.

解决方案

Your cboPO control is already bind with datasource that display data

use this if want to add rows in your combobox

DataTable dt = cboPO.Datasource;
DataRow dr = dt.NewRow();
dr["ColumnName"/Index number] = cboPO.Text;

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

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