Getting the "new row" to appear in a DataGridView

Getting the "new row" to appear in a DataGridView

Old forum URL: forums.lhotka.net/forums/t/360.aspx


BBM posted on Tuesday, June 13, 2006

Hi everyone,

I'm writing an application that has a UserControl that is basically a clone of the Project Tracker RolesEdit control.  I've gotten it to work (all the stuff about setting AllowNew to true in the Editable Root collection constructor, and overriding AddNewCore()), but I have one difference between my code and Project Tracker that I don't understand.

The difference is the setting of the AllowNew property on the BindingSource object in the UserControl designer  (RolesBindingSource on the RolesEdit designer).  In Project Tracker it's set to false, but I can't seem to get mine to work without setting this value to true in the designer.

I figured that maybe this value was being manipulated in WinPart, or maybe in the code behind of the UserControl, but I can't find anyplace in ProjectTracker that this property is being set.

I suspect that I'm setting myself up for future trouble here by making the "new row" always show, even if the user is not authorized to add rows.  So I'm considering adding code to the "load" routine of the UserControl to set this value based on the user's authority and the AllowNew setting of the DataSource.  I obviously don't want to do this if I can help it.

Can someone educate me on this?  Does DataBinding or WinPart or anything automatically manipulate the value of AllowNew in the BindingSource?  Or do I have something else set up wrong?

Thanks for your help.

BBM

 

BBM replied on Thursday, June 15, 2006

Here's what I've found out so far:

1)  The AllowNew property of the RolesBindingSource in Project Tracker is set to true by the BindingSource object itself when an object that implements IBindingList (like BusinessListBase (of T,C)) is assigned to the DataSource property.  If AllowNew is set to true for the object that is assigned as the DataSource, the AllowNew property of the BindingSource will automatically be set to true.  There are some cases where it won't be set, like if the DataSource object is fixed length, or doesn't implement a default constructor.  The documentation for BindingSource covers these.

2)  That having been said, my code doesn't work that way.  I think my code mirrors that in Project Tracker (none of the exception conditions seem to apply,  my collection is a BusinessListBase (of T, C) and AllowNew is set to true on instantiation) but when the assignment is made, AllowNew for the BindingSource object stays false.  I can rectify this by just setting the value myself after the DataSource assignment, and in fact, I think this improves the code because you have the option to check to see if the user can add objects and you can leave it false if he/she doesn't have the authority.

So for now, unless someone else has any ideas, I'm considering this to be a quirk of the BindingSource class (although I'm open to the idea that it's not working because of some other bonehead thing I've done).  I'll explicitly set AllowNew on the BindingSource to what I need when I set the DataSource.

Thanks to everyone who peeked at my problem to see if they could help.

BBM

 

cmay replied on Friday, June 16, 2006

BBM

Thanks for writing what you found out b/c I was going through a similar situation.  I gave up and created an "Add New" button that adds a new item to my list that is data bound, because I couldn't figure this out.

Thanks again.

cmay replied on Saturday, June 17, 2006

Hey BBM,

I tried your method of setting the AllowNew on the collection = true, but I am still getting the exception that there is no available constructor for my collected object (because of factory usage).

Do you have to create a public constructor in order to get this to work?

BBM replied on Sunday, June 18, 2006

Rocky covers this in the VB book on page 452.  You have to override the AddNewCore method on the collection object so that it can add the object for the "new row".  This method just calls the factory New  method in the "collected" type.

So the collected type constructors are private.

Hth.

BBM

 

cmay replied on Monday, June 19, 2006

Thanks BMM!

For anyone interested this is on page 450 of the C# book.

Copyright (c) Marimer LLC