Gabriele
Gabriele

Reputation: 466

BindingList AddingNew event not fired

I'm struggling with the AddingNew event on BindingList. This is my code: the ListChanged is fired, the AddingNew not. I'm missing something?

{
    //....
    System.ComponentModel.BindingList<string> test = new System.ComponentModel.BindingList<string>();
    test.AllowNew = true;
    test.RaiseListChangedEvents = true;
    test.AddingNew += Test_AddingNew;
    test.ListChanged += Test_ListChanged;
    test.Add(new string("test1"));
    test.Add("test2");
    //....
}

private void Test_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
{
    throw new NotImplementedException();
}

private void Test_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
{
    throw new NotImplementedException();
}

Upvotes: 1

Views: 648

Answers (2)

Troy Mac1ure
Troy Mac1ure

Reputation: 647

I know this is old, but I was having the same issue. AddingNew is ONLY fired when BindingList.AddNew() is called. It is not called when BindingList.Add() is called.

Therefore either .ListChanged will need to be used or AddNew() will be required to fire the event.

Upvotes: 3

Sandris B
Sandris B

Reputation: 133

Its working for me, but what is this? - new string("test1") <- remove it

class Program
{
    static void Main(string[] args)
    {
        //....
        System.ComponentModel.BindingList<string> test = new System.ComponentModel.BindingList<string>();
        test.AllowNew = true;
        test.RaiseListChangedEvents = true;
        test.AddingNew += Test_AddingNew;
        test.ListChanged += Test_ListChanged;
        test.Add("test1");
        test.Add("test2");
    }

    private static void Test_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
    {
        throw new NotImplementedException();
    }

    private static void Test_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
    {
        throw new NotImplementedException();
    }
}

Upvotes: -1

Related Questions