468,554 Members | 1,435 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

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

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
Aug 29 '08 #1
3 9170
On Aug 29, 1:54*pm, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
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.
Aug 29 '08 #2
I'm actually looking for the same result. When a user navigates to the 'new row' field in the datagrid view, but does not type anything in, a new object is still created in my BindingList. My constructor had to be designed perfectly to allow a new item without any user input, but it would be a bad thing to let this get into the database.. so I'd rather not allow it.

I tried overriding the BindingList methods, but I can't seem to figure it out. I'm trying to keep track of the InsertedItem as a member variable and only adding it when the row is committed. Is this the approach you took?
Sep 12 '08 #3
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
Sep 12 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by JochenZ | last post: by
reply views Thread by Chris Dunaway | last post: by
reply views Thread by AndyM | last post: by
4 posts views Thread by Michel Walsh | last post: by
2 posts views Thread by csharpula csharp | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.