当datasource属性设置错误时,无法修改项集合 [英] Items collection cannot be modified when the datasource property is set error
问题描述
我有两个窗口窗口
错误显示我需要从文本框中的Windows form2传递数据
到windows form1组合框
第一个窗体
找到windows form1中的组合框,我在表格的加载事件中显示数据
I have two windows form
error show when i need to pass data from windows form2 in textbox
to windows form1 combobox
First windows Form
found combobox inside windows form1 and I show data in it in load event of form
public DataTable ShowExceltwoLanguage()
{
OleDbConnection con = new OleDbConnection(connection);
con.Open();
string str = "select MemberID,MemberNameAR + ' - ' + MemberNameEN as MemberName from [MemberAR$]";
OleDbCommand com = new OleDbCommand();
com = new OleDbCommand(str, con);
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
ds = new DataSet();
oledbda.Fill(ds, "[MemberAR$]");
con.Close();
DataTable dt = new DataTable();
dt = ds.Tables["[MemberAR$]"];
return dt;
}
在windows form1的加载事件中
我写如下
in load event of windows form1 i write as following
QrClasses qrc = new QrClasses();
DataTable dt = qrc.ShowExceltwoLanguage();
comboBox4.DataSource = dt;
comboBox4.DisplayMember = "MemberName";
comboBox4.ValueMember = "MemberID";
第二个窗口形式
这里是错误
设置DataSource属性时无法修改项目集合显示
$ 2 $ b在button2点击事件的按钮1中
Second windows form
here is error
Items collection cannot be modified when the DataSource property is set show
in button1 of form2 click event
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
var cb = ((Form1)Owner).comboBox4;
var index = cb.FindString(textBox1.Text);
if (index == -1)
{
cb.Items.Add(textBox1.Text);// in this line Additional information: Items collection cannot be modified when the DataSource property is set.
index = cb.FindString(textBox1.Text);
if (index > -1)
{
cb.SelectedIndex = index;
Close();
}
}
else
{
cb.SelectedIndex = index;
}
}
以便在可能的情况下如何解决?
我尝试了什么:
so that how to solve that if possible ?
What I have tried:
Items collection cannot be modified when the DataSource property is set Error
推荐答案
;
OleDbCommand com = new OleDbCommand();
com = new OleDbCommand(str,con);
OleDbDataAdapter oledbda = new OleDbDataAdapter();
oledbda = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
ds = new DataSet();
oledbda.Fill(ds, [MemberAR
"; OleDbCommand com = new OleDbCommand(); com = new OleDbCommand(str, con); OleDbDataAdapter oledbda = new OleDbDataAdapter(); oledbda = new OleDbDataAdapter(com); DataSet ds = new DataSet(); ds = new DataSet(); oledbda.Fill(ds, "[MemberAR
< /跨度>);
con.Close();
DataTable dt = new DataTable();
dt = ds.Tables [ [MemberAR
"); con.Close(); DataTable dt = new DataTable(); dt = ds.Tables["[MemberAR
< /跨度>];
return dt;
}
"]; return dt; }
在windows form1的加载事件中
我写如下
in load event of windows form1 i write as following
QrClasses qrc = new QrClasses();
DataTable dt = qrc.ShowExceltwoLanguage();
comboBox4.DataSource = dt;
comboBox4.DisplayMember = "MemberName";
comboBox4.ValueMember = "MemberID";
第二个窗口形式
这里是错误
设置DataSource属性时无法修改项目集合显示
$ 2 $ b在button2点击事件的按钮1中
Second windows form
here is error
Items collection cannot be modified when the DataSource property is set show
in button1 of form2 click event
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
var cb = ((Form1)Owner).comboBox4;
var index = cb.FindString(textBox1.Text);
if (index == -1)
{
cb.Items.Add(textBox1.Text);// in this line Additional information: Items collection cannot be modified when the DataSource property is set.
index = cb.FindString(textBox1.Text);
if (index > -1)
{
cb.SelectedIndex = index;
Close();
}
}
else
{
cb.SelectedIndex = index;
}
}
以便在可能的情况下如何解决?
我尝试了什么:
so that how to solve that if possible ?
What I have tried:
Items collection cannot be modified when the DataSource property is set Error
这篇关于当datasource属性设置错误时,无法修改项集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!