Question

I have to forms.

First has datagridview with BindingList<CustomObject> as DataSource.

Second should add/remove/update DataSource from the first form.

How can I do this? Modifying etc is happening in button_Click(object sender, EventArgs e) on secondform. I could pass BindignList<> by ref to SecondForms() constructor, but I can't pass it further to button_Click()

Was it helpful?

Solution

What you can do is create an event in form2 that form1 will subscribe to. Keeping things sort of separate. I don't know how you have structured Form1 and Form2 so I will just give an example.

class Form2 : Something
{
  public event NotifySubscriberEventHandler NotifySubscriberEvent ;
   public void button_Click(object sender, EventArgs e)
   {
      var handler = NotifySubscriberEvent ;
      if( handler != null)
       {
          handler(this,EventArgs.Empty) ;
       } 

   } 
} 

class Form1 
{
   public BindingList<T> MyBindingList {get;set;} //
   public void CreateForm2()
   {
       Form2 form2 = new Form2() ; 
       form2.NotifySubscriberEvent += OnButtonClicked;

   }
   public void OnButtonClicked(object source, EventArgs e)
   {
     //Do Something when notified
      MyBindingList.Add(...)
   }
}

You will have to create a NotifySubsubscriberEventHandler delegate. Here: http://www.akadia.com/services/dotnet_delegates_and_events.html#Simple%20Event

But you already say you are passing BindingList into a constructor I assume like this:

public class Form2
{
  private BindingList<T> bindingList ;
  public Form2(BindingList<T> bindingList)
  {
       this.bindingList = bindingList ;
  }

   public void button_Click(object sender, EventArgs e)
   {
   // Do     bindingList.Add() or whatever

   } 
}

Does the above not work? ^^

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top