Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm bind
My Tables in dataset don't accept null value.
There is DataBound ListBox and a BindingNavigator. They have a same BindingSource. When I Click on AddNewItem a dialog will open to get entries from user.
 
public void AddNewItem_Click(object sender, EventArgs e)
{
    CustomerDialog dialog = new CustomerDialog();
 
    if (dialog.ShowDialog() == DialogResult.Cancel)
    {
        this.Customer.Items.RemoveAt(this.Customer.Items.Count - 1);// Error
        return;
    }
    Data.VMSDataSet.CustomerRow row = this.VMSDataSet.Customer.NewCustomerRow();
 
    row.FirstName = dialog.FirstName;
    row.LastName = dialog.LastName;
 
    this.VMSDataSet.Customer.Rows.Add(row);
}
 
At specified line I've got this error:
Items collection cannot be modified when the DataSource property is set.
 
How can I avoid it?
 
(Note: I had same problem with DataGridView before, in that case I write the below code without error.)
this.GridView.Rows.RemoveAt(this.GridView.Rows.Count - 1);
Posted 20-Aug-12 9:00am
Edited 25-Aug-12 0:55am
v9
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Handling this kind of exception does not make any practical sense. You rather need to prevent it.
 
First of all, this type of exceptions is one of the easiest to detect, to fix the problem. The nature of it is that you try to dereference a null reference. A work-around should be based on elimination of such dereferencing, which never makes sense. You can make sure that some variable or member is always initialized to refer to some actually existing (created) object. Alternatively, test а variable or member in question for null and, if it is null, don't do any operations using any of its instance members. All other cases make no sense.
 
—SA
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 21-Aug-12 13:13pm
   
Not only you should not handle the exception, you don't need to handle it, because you shall avoid it. So your question could be about how to avoid throwing this exception, nothing else. Both of your RemoveAt calls are valid, because the indices go 0 .. Count-1 -- they remove last item; so the problem could be somewhere else.
Your "does not work" is not descriptive. This is not a language a developer should use. What, same exception?
After all, create a complete but minimal code sample and try to reproduce this problem only, isolated from everything else. If you cannot see how to work it out, show this sample using "Improve question".
--SA
Sergey Alexandrovich Kryukov at 22-Aug-12 0:48am
   
I trust you, just don't understand. A logic error... Exception full type and message, please... :-)
No exception? Then what is that error, exactly?
--SA
Sergey Alexandrovich Kryukov at 25-Aug-12 2:10am
   
Now it's clear. Isn't it clear enough? DataSource property should be null to be able to change the GridView. Or modify your grid view through binding only. The control does not allow you to do both ways. This is pretty much understandable; as it would create a disaster. What to do? Depending on what you want.
--SA
Sergey Alexandrovich Kryukov at 5-Sep-12 3:29am
   
No, I got it, only did not see exact scenario which causes the problem. At the same time, if you look at your own fix, you will see that it follows my general recommendation to avoid mixing binding with direct manipulation with the control's data -- please see my comment above.
 
Anyway, it's great that you did the right fix.
Good luck,
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

list box data cannot be remove
  Permalink  
Comments
ThePhantomUpvoter at 12-Jul-13 11:59am
   
Really? And can you cite a reference for this inability to ever remove data from list boxes?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 548
1 CHill60 390
2 OriginalGriff 323
3 BillWoodruff 213
4 PIEBALDconsult 185


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 12 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100