The previous day, I posted a piece of entity and entity set Code .
1. entity class
Public Class Orderinfo: ieditableobject, idataerrorinfo
The object class inherits the ieditableobject and idataerrorinfo interfaces.
ieditableobject is used in data binding. It mainly includes three functions: beginedit (), canceledit (), and endedit ().
when the collection object of the object class is bound to a DataGrid, if you click the last row in the DataGrid (winform) with the mouse, a new object is created, in addition, the DataGrid automatically calls the beginedit method in the ieditableobject interface of the object class. If you do not need to add one, press the ESC key on the disk to cancel typing. This DataGrid will automatically call the canceledit method in the ieditableobeject interface to cancel typing. In my code, a removeme event is triggered to the Collection object of the object class. At this time, orderinfocollection will remove the newly added orderinfo.
Of course, you can also use the three ieditable methods to perform rollback. For example, you can manually call beginedit to modify the data, and then use endedit to submit the modified data. If the modification is incorrect, you can use canceledit to roll back and forth.
for the idataerrorinfo interface, it has only two attributes: Public string error and Public string This [ string strerrorname], is actually used for data binding, if the added or modified data is incorrect (you can write a specific judgment in the error get method and return an error message), that is, when the error attribute is not equal, the DataGrid (which is also a winform) will provide the specific error information (which is actually the value of the error attribute) at the beginning of the row, without additional encoding. Of course, other data bindings can also achieve this effect.
There is anotherInternal ClassOrderinfoeventargs: eventargs is an empty class, but it is not needed for future expansion.
2. Collection of object classes
Public Class Orderinfocollection: collectionbase, ibindinglist
For an object set, it inherits from the collectionbase class and the ibindinglist interface.
Inherit from collectionbase to complete functions similar to arraylist, such as ADD, clear, remove, and the new getenumerator () iteration method, so that you can use the orderinfocollection class in foreach.
The entity set implements the ibindinglist interface to bind data, including sorting and searching. For example, if orderinfocollection is bound to a DataGrid (winform), when you click the bottom empty line, if the allownew attribute of orderinfocollection returns true, it will automatically callObjectThe ibindinglist. addnew () method is used to create a new orderinfo, And the beginedit method of orderinfo is automatically called (if the ieditableobject interface is implemented in orderinfo ).
In addition, a listchanged event is added. This event is triggered when an operation is performed on the Set (after the modification and deletion are completed ).