Adding New Rows in DataGridView when Bound to BindingList<T>

J

jehugaleahsa

Hello:

I am binding a DataGridView with a BindingList<T>, where T is a custom
business object that implements INotifyPropertyChanged.

When you bind a DataGridView to a DataTable, it has this cool little
feature - it will not call DataTable.Rows.Add until after you leave
the DataGridView row. This is cool because it lets your user edit the
record as much as needed to get it into a valid state before actually
adding it to the DataTable.

I obviously loose this somewhere when I switch to my custom business
object. Is this something I have to implement at the business object
level or at the BindingList level? And either way, can someone give me
an example implementation?

Thanks,
Travis
 
J

jehugaleahsa

Hello:

I am binding a DataGridView with a BindingList<T>, where T is a custom
business object that implements INotifyPropertyChanged.

When you bind a DataGridView to a DataTable, it has this cool little
feature - it will not call DataTable.Rows.Add until after you leave
the DataGridView row. This is cool because it lets your user edit the
record as much as needed to get it into a valid state before actually
adding it to the DataTable.

I obviously loose this somewhere when I switch to my custom business
object. Is this something I have to implement at the business object
level or at the BindingList level? And either way, can someone give me
an example implementation?

Thanks,
Travis

I was able to find *a* solution by looking at MSDN. I looked at
System.ComponentModel.ICancelAddNew. I just had to override
BindingList<T>'s CancelNew, InsertItem, and EndNew methods.

I needed to do this because as soon as an item was added to a
DataGridView, it needed to be immediately added to a DataTable as a
row. But, instead of just binding directly to a DataTable, I create a
mapper to help separate my Data layer from my Business layer. I have a
BindingList class that will automatically take changes and map those
to DataTable using the mapper (such as adding, removing and updating
the business objects).

However, it was hard to use my BindingList because I couldn't get the
AddNew semantics to match DataView's. Now I think I have achieved that
goal.
 
M

Marc Gravell

What exactly do you want to do? For example, you can override AddNewCore
to get hold of newly editing objects - and you can also use this method
to avoid having to have a default constructor if you want (i.e. this
method can initialize your objects).

However, catching when lines are committed/aborted is annoyingly glitchy...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top