DataGridView with Custom Object List throws NotImplementedException when Editing
I am building a 开发者_开发问答WinForms application with a few custom business objects. I bind a List of a single type of object to a DataGridView with BindingList and implement IEditableObject on my object, but when I attempt to edit the values in the DataGridView I get the following exception:
The following exception occurred in the DataGridView:
System.NotImplementedException: The method or operation is not implemented. - at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component,Object value) - at System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)
To replace this default dialog, please handle the DataError event.
I'm sure I'm missing something, but I don't know what it is...
The error message tells you all you need to know - you need to handle the DataError
event of your DataGridView
.
The MSDN page I linked above also links to an article on Consuming Events that may be helpful as well:
To consume an event in an application, you must provide an event handler (an event-handling method) that executes program logic in response to the event and register the event handler with the event source. This process is referred to as event wiring. The visual designers for Windows Forms and Web Forms provide rapid application development (RAD) tools that simplify or hide the details of event wiring.
Well, apparently I'm not very observant...
The problem was that ReSharper implemented my properties with NotImplementedException, so when I edited that property through the DataGridView, it threw that Exception.
Thanks, sorry to waste your (and my!) time.
I had an exception with a list of custom objects with 'Index - 1':
List<Object> newObjList = new List<Object>();
OR
IList<Object> newObjList = new List<Object>();
Was assigning the quick and dirty way:
dgv.DataSource = null //this is user preferred way to refresh list
dgv.DataSource = newObjList
Resolved this by casting the datasource assignment to an array:
dgv.DataSource = newObject.ToArray<Object>();
精彩评论