Custom BindingList<T> question

Hello;

I have a question related to implement a Custom BindingList<T>, I'm using .NET framework Beta 2.
My custom class is declared as:
public class CustomBindingList<T>: BindingList<T>

Using my custom class I see that it expose a AddNew() method which returns to me new initialized item.
Reading the help for this method gave an understanding on how this method works, but in really in my code it works different, so I'm not really sure if I just misunderstand it. From help I understand the following behaviour:
1.- Calling AddNew returns me a new item, but it's not added to Items internal list until I explicitly call EndNew method, or it's never added if I call CancelNew. If I'm right what happens with the Count property of the binding list after AddNew, but before EndNew or CancelNew

From my code I can see the following behaviour:
1.- Calling Addnew it returns a new Item, the AddeNew it's not overrided in my implementation, but AddNewCore is:
        protected override object AddNewCore()
        {
            return base.AddNewCore();
        }
1.1.- After calling base.AddNewCore I saw that binding list call InsertItem, which is overrided:
        protected override void InsertItem(int index, T item)
        {
            base.InsertItem(index, item);
        }
       
But my question is why does it call InsertItem, if I understand right it must be called after EndNew.

The code that use my custom collection is:

            BindableCollectionBase<TestEntity> col = new BindableCollectionBase<TestEntity>();

            TestEntity newItem = col.AddNew(); // Inside this method it calls the InsertItem method
            newItem.Name = "AllowNewTrueCollectionIBindingTest";
            Console.WriteLine(String.Format("Adding item: {0}", col.Count)); // This line displays that my collection has one item
            col.EndNew(col.IndexOf(newItem)); // I'm expecting to have one item, but after calling this method.
           
Any help would be really appreciated.
Thanks
Mario Chavez

 




Answer this question

Custom BindingList<T> question

  • jbrunken

    Scott;

    Thanks for your response, iti did clarify this behaviour to me.

    Saludos.
    Mario Alberto

  • ucci

    When you call AddNew, the item is actually added, but not yet confirmed.  It remembers the position of the item you added as the suspect new item that may or may not be canceled.  EndNew confirms that you don't want to cancel the item (so it no longer tracks this position as suspect), while CancelNew removes it.

    If you bind a DataGridView to your list, you can see the effect of ICancelAddNew: if you go to a new row and start typing, you can hit escape to remove the new row entirely (CancelNew).  If instead you click on a different row (implicitly calling EndNew), then move back to the row you had added, hitting escape won't get rid of the row anymore.
       -Scott


  • Custom BindingList<T> question