1
Reply

Items collection cannot be modified when the DataSource prop

ahmed salah

ahmed salah

Feb 6 2017 9:43 AM
271
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
 
  1. public DataTable ShowExceltwoLanguage()  
  2. {  
  3.   
  4. OleDbConnection con = new OleDbConnection(connection);  
  5.   
  6.   
  7. con.Open();  
  8. string str = "select MemberID,MemberNameAR + ' - ' + MemberNameEN as MemberName from [MemberAR$]";  
  9. OleDbCommand com = new OleDbCommand();  
  10. com = new OleDbCommand(str, con);  
  11. OleDbDataAdapter oledbda = new OleDbDataAdapter();  
  12. oledbda = new OleDbDataAdapter(com);  
  13. DataSet ds = new DataSet();  
  14. ds = new DataSet();  
  15. oledbda.Fill(ds, "[MemberAR$]");  
  16. con.Close();  
  17. DataTable dt = new DataTable();  
  18. dt = ds.Tables["[MemberAR$]"];  
  19. return dt;  
  20.   
  21.   
  22. }  
  23. in load event of windows form1 i write as following  
  24. QrClasses qrc = new QrClasses();  
  25. DataTable dt = qrc.ShowExceltwoLanguage();  
  26. comboBox4.DataSource = dt;  
  27. comboBox4.DisplayMember = "MemberName";  
  28. comboBox4.ValueMember = "MemberID";  
Second windows form
here is error
 
Items collection cannot be modified when the DataSource property is set show
in button1 of form2 click event
 
  1. if (!string.IsNullOrWhiteSpace(textBox1.Text))  
  2. {  
  3.   
  4. var cb = ((Form1)Owner).comboBox4;  
  5. var index = cb.FindString(textBox1.Text);  
  6. if (index == -1)  
  7. {  
  8. cb.Items.Add(textBox1.Text);// in this line Additional information: Items collection cannot be modified when the DataSource property is set.  
  9. index = cb.FindString(textBox1.Text);  
  10. if (index > -1)  
  11. {  
  12. cb.SelectedIndex = index;  
  13. Close();  
  14. }  
  15. }  
  16. else  
  17. {  
  18.   
  19. cb.SelectedIndex = index;  
  20. }  
  21. }  
so that how to solve this problem  if possible?

Answers (1)