But I have Problem I can't binding data in Form Load Event Athough When Form display If I click Button call to Method Binding then Data is binding to Data Grid View.
public class EmployeeModel : INotifyPropertyChanged
{
#region "Define Variable"
private NorthwindEntities _mgr = new NorthwindEntities();
public ObservableCollection<Employee> Employees { get; private set; }
public ObservableCollection<string> Log { get; private set; }
#endregion
#region "Property Event"
public event PropertyChangedEventHandler PropertyChanged = delegate { };
#endregion
#region "Define Property"
private string _busyPropertyName;
private bool _isBusy;
public bool IsBusy
{
get { return _isBusy; }
set
{
_isBusy = value; RaisePropertyChanged(_busyPropertyName);
}
}
public bool IsOrderBusy { get { return _isBusy; } }
private Employee _currentEmployee;
public Employee CurrentEmployee
{
get { return _currentEmployee; }
set { _currentEmployee = value; RaisePropertyChanged("CurrentEmployee"); }
}
private int _currentPosition = -1;
public int CurrentPosition
{
get { return _currentPosition; }
set
{
if (Employees != null && Employees.Count > 0)
{
_currentPosition = Math.Min(Math.Max(value, 0), Employees.Count - 1);
CurrentEmployee = Employees[_currentPosition];
}
else
{
_currentPosition = -1;
CurrentEmployee = null;
}
RaisePropertyChanged("CurrentPosition");
}
}
private bool _hasChanges = false;
public bool HasChanges
{
get { return _hasChanges; }
private set
{
_hasChanges = value;
RaisePropertyChanged("HasChanges");
}
}
private EntityQuery<Employee> _query;
#endregion
#region "Construction"
public EmployeeModel()
{
Employees = new ObservableCollection<Employee>();
Employees.CollectionChanged += Employees_CollectionChanged;
Log = new ObservableCollection<string>();
_busyPropertyName = "IsBusy";
_mgr.Fetching += (s, e) =>
{
IsBusy = true;
WriteToLog(e.Query);
};
_mgr.Queried += (s, e) =>
{
IsBusy = false;
_busyPropertyName = "IsOrderBusy";
WriteToLog("Query returned " + e.Results.Cast<object>().Count());
};
//_mgr.EntityChanged += (s, args) => HasChanges = ((NorthwindEntities)s).HasChanges();
_query = _mgr.Employees;
var op = _query.ExecuteAsync();
op.Completed += GotEmployees;
//_query.ExecuteAsync(
// op =>
// {
// if (op.HasError)
// {
// throw new InvalidOperationException("Query failed: " + op.Error);
// }
// op.Results.ForEach(Employees.Add);
// CurrentEmployee = Employees.FirstOrDefault();
// });
}
#endregion
#region "Write Log"
private void WriteToLog(IEntityQuery query)
{
var returnType = query.ElementType;
var message = (returnType == typeof(Order))
? "Fetching Orders from Employee " + CurrentEmployee.EmployeeID
: "Fetching " + returnType;
WriteToLog(message);
}
private void WriteToLog(string message)
{
Log.Insert(0, message);
}
#endregion
#region "Entity Method"
private void GotEmployees(object sender, EntityQueriedEventArgs<Employee> args)
{
if (args.HasError)
{
throw new InvalidOperationException("Query failed: " + args.Error);
}
args.Results.ForEach(Employees.Add);
CurrentEmployee = Employees.FirstOrDefault();
}
public void Save()
{
_mgr.SaveChangesAsync();
}
private void Add(System.Collections.Generic.IEnumerable<Employee> employees)
{
//_mgr.AddEntities(employees);
//Save();
//employees.ForEach(_mgr.AddEntity);
if (employees.Any(emp => emp.EntityAspect.EntityState.IsDetached()))
{
_mgr.AddEntities(employees);
}
}
private void Delete(IEnumerable<Employee> employees)
{
employees.ForEach(emp => emp.EntityAspect.Delete());
Save();
}
private void Employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
Add(e.NewItems.Cast<Employee>());
break;
case NotifyCollectionChangedAction.Remove:
Delete(e.OldItems.Cast<Employee>());
break;
}
}
public void RollbackChanges()
{
int privateCurrentPosition = CurrentPosition;
_mgr.RejectChanges();
Employees.Clear();
_query.With(QueryStrategy.CacheOnly).Execute().ForEach(Employees.Add);
CurrentPosition = privateCurrentPosition;
}
#endregion
#region "Private Method"
private void RaisePropertyChanged(string property)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
void _mgr_Fetching(object sender, EntityFetchingEventArgs e)
{
WriteToLog("Fetching " + e.Query.ElementType);
}
#endregion
}