472,954 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

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 9588
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
0
by: JochenZ | last post by:
Hello, I have a DataGrid(View) and a DataTable. The DataTable is displayed in the DataGridView. The user is allowed to select some rows and then the following snippet updates some values in the...
7
by: Robert Koller | last post by:
Hello i have this problem: In a DataGridView the user work with data from a DataSet. on Start the row index from datagridview is the same as the row index from the dataset. Also: ...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
0
by: Chris Dunaway | last post by:
I posted this to the xml group but got no responses, so I thought I'd try here. I have the following small class which inherits from the generic List class: public class FoodItemList :...
0
by: AndyM | last post by:
I have tried to create an object that is a binding list where the value of T varies and so cannot be hard coded. I used the override for CreateInstance which takes a type object. The Type was...
4
by: Michel Walsh | last post by:
Hi, A datagrid has its DataSource set to a BindingList<CustomClass>. While the ListChanged event fires AFTER a deletion, I am looking for a simple way to be notified BEFORE the item would be...
2
by: csharpula csharp | last post by:
Hello, I would like to know what is better for data binding and serialization purposes ArrayList or List<? Thank you! *** Sent via Developersdex http://www.developersdex.com ***
22
by: seagullino | last post by:
Hello, I have a form that includes a field for segmenting a table by customer "rating." The ratings are A, B, C, and Not Rated. Here's how I'd like it to work: If the user selects "C" as...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.