New Posts New Posts RSS Feed: Data Binding in Windows Form
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Data Binding in Windows Form

 Post Reply Post Reply
Author
duyhungb5 View Drop Down
Newbie
Newbie
Avatar

Joined: 11-Jul-2012
Posts: 1
Post Options Post Options   Quote duyhungb5 Quote  Post ReplyReply Direct Link To This Post Topic: Data Binding in Windows Form
    Posted: 11-Jul-2012 at 2:59am
I have using DevForce create small Application in WinForm
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.
 EmployeeModel viewModel;
        public Form1()
        {
            InitializeComponent();
            viewModel = new EmployeeModel();
           
        }

//Event Is Ok
        private void button1_Click(object sender, EventArgs e)
        {
            this.employeeBindingSource.DataSource = viewModel.Employees;
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = this.employeeBindingSource;
           
        }

//Event Is not Display Data in Data GridView
        private void Form1_Load(object sender, EventArgs e)
        {
            
            this.dataGridView1.DataSource = viewModel.Employees;
            dataGridView1.Refresh();            
            
        }

I have create class using DevForce
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

    }


Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jul-2012 at 11:11am
Hi,
 
Have you taken a look at our WinForms sample? You can download it in the DevForce Resource Center.
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down