473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 9653
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.