_Books = new ObservableCollection<IRecordModel>(); for (int i = 0; i < 10; i++) { Book book = new Book(); book.Id = i; book.Title = string.Format("Title {0}", i); book.Author = string.Format("Author {0}", i); book.BindingType = BindingType.Hardback; book.IsOnSale = true; book.Price = (decimal)(i * 10.1); book.Pubdate = DateTime.Now; _Books.Add(book); } // reoGridControl is the ReoGridControl control instance _WorksheetModel = new WorksheetModel(reoGridControl, typeof(Book), _Books); //If you want to check the validity of the input variables, you can implement the function. _WorksheetModel.OnBeforeChangeRecord += WorksheetModel_OnBeforeChangeRecord;
4.3 implement the WorksheetModel_OnBeforeChangeRecord
1 2 3 4 5 6 7 8 9 10 11 12 13 14
privatebool? WorksheetModel_OnBeforeChangeRecord(IRecordModel record, System.Reflection.PropertyInfo propertyInfo, object newProperyValue) { if (propertyInfo.Name.Equals("Price")) { decimal price = Convert.ToDecimal(newProperyValue); if (price > 100m) //assume the max price is 100 { MessageBox.Show("Max price is 100.", "Alert", MessageBoxButton.OK, MessageBoxImage.Warning); returntrue; // cancel the change } }
// add a book int count = _Books.Count; Book book = new Book(); book.Id = count; book.Title = string.Format("Title {0}", count); book.Author = string.Format("Author {0}", count); book.BindingType = BindingType.Hardback; book.IsOnSale = true; book.Price = (decimal)(count * 10.11) > 100m ? 100m :(decimal)(count * 10.11); book.Pubdate = DateTime.Now; _Books.Add(book);
// remove a book if (_Books.Count > 0) { _Books.RemoveAt(_Books.Count - 1); }
// move a book if (_Books.Count > 2) { _Books.Move(0, _Books.Count - 1); }
// edit a book (_Books[0] as Book).Price = new Random(DateTime.Now.Millisecond).Next(1,100); _WorksheetModel.UpadteRecord(_Books[0]); // invoke UpadteRecord after editing one record.