Best Binding Method

Hi,

i am creating a user control and wish to know the best way to databind an poco class?

Do i use a form view to bind a single object? or is there a better / easier way?

Can anyone help?
cossy74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guveraCommented:
Hi,

 Which architecture?

For 1-way binding, you don't need anything other than public properties - and maybe some TypeConverter implementations for any bespoke data types (structs etc)

For full 2-way binding, you'll need an eventing implementation - any of:

=> a "public event EventHandler FooChanged" for every property "Foo"
=> an `INotifyPropertyChanged implementation
=> a bespoke component-model (don't go there - overkill)
For an example of a INotifyPropertyChanged implementation (note you might want to move some of the code for re-use) :



public class Foo : INotifyPropertyChanged
{    
private string bar;  
public string Bar    
 {        
 get { return bar; }        
  set { UpdateField(ref bar, value, "Bar");
 }    
 }     // other properties...    
  public event PropertyChangedEventHandler PropertyChanged;  
  protected virtual void OnPropertyChanged(string propertyName)    
  {        
   if (PropertyChanged != null) PropertyChanged(this,            
      new PropertyChangedEventArgs(propertyName));  
  }    
 protected bool UpdateField<T>(ref T field, T value,         string propertyName)  
  {
         if (!EqualityComparer<T>.Default.Equals(field, value))        
          {            
              field = value;            
              OnPropertyChanged(propertyName);            
              return true;        
           }        
        return false;  
  }
 }

To bind sets of data (grids etc), the easiest thing is to use generics; basically, the minumum is IList - but you get extra metadata from a public T this[int index] indexer - which List<T>, Collection<T> etc all have. More - BindingList<T> implements IBindingList allowing collection-based notification events (but only to INotifyPropertyChanged - not to the FooChanged pattern).

Thanks
Guvera

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.