I have an aspx. page that I'm using as an EditForm for a Radgrid. The Radgrid is contained on this page as a UserControl. Therefore, I have a button on this page that I'm using to direct the appropriate commandname needed in the control's code-behind. This works fine to edit/update a record from the Radgrid. However, I can't wrap my head around getting this to work for Inserting a record. The following is my button click event that creates an error when inserting ("Index was outside the bounds of the array")
Any help is much appreciated.
9 Answers, 1 is accepted
Make sure that you have set the property CommandItemDisplay to any values other than "None".
Somehow I have to let the grid know that I'm inserting a record, and to just fire the PerformInsert Command which I can't seem to figure out.
Since you are accessing the CommandItem to perform Insert, its required to set CommandItemDisplay. One work around would be to set it and hide it. Here is a sample that i tried to make the Grid in edit and perform Insert. Provide your full code snippet if this doesn't help.
You could use buttons placed outside RadGrid to insert and update items. In order to do this you could use similar approach to the one suggested by Princy, however without the CommandItem.
OnClick event of a Button you could get the grid EditFormItems, check if an item is GridEditFormInsertItem and fire Insert command:
I am also attaching a sample project as reference. Give this approach a try and you should achieve the behavior you are looking for.
Hi again Princy - that's basically what I've got to a 'T' (except for adding the aforementioned CommandItemDisplay which I did include). And the small difference of how I go into it differently depending on whether Insert or Update. Your 'button2' is essentially the same as the following rbtnAddNew.
SaveChanges_Click is essentially the same as your
Button1_Click (just a different method of discerning modes).
Briefly the grid looks like:
Unfortunately, I'm still getting a 'Index is outside the bounds of the arrray' error. So I'm not sure if the above helps any, but thanks for your efforts.
I may just transpose the entire block of code from my usercontrol insert command to the SaveChanges_Click event of the 'parent' page.
In my instance with the grid being a usercontrol and mainpage being essentially the edit form, the 'GridItem item
gridItems' is empty unless I have a method of filling this. I looked at your attached example as well, but couldn't see how that was done. Perhaps I'm missing something.
Thank you for writing back.
The gridItems collection from my previous example contains all items that are EditFormItem. The collection is returned by the GetItems() method.
From the markup you provided for RadGrid it seems that you are using the grid's default EditForm settings. I am not quite sure what you mean by that the main page being the edit form. Would you elaborate more on the scenario you have?
I have prepared another project that illustrates how the insert command could be fired from the server and from the client. Give the attached project a try and let me know if it is working for you.
Would you also let me know what changes are required to the sample so it resembles a simplified version of your scenario? This would allow us to have better understanding of your case.
My scenario was basically modelled after an example on the Telerik demo site for the 2013 Q2 version (with something to the effect of 'Using an alternate edit form'). It's basically the opposite of other examples you have whereby the edit form is a usercontrol. Attached is a screen shot if that helps visualize what I'm trying to do (this screen is the alternate edit form; grid below 'Search Results' is grid as UserControl). I had everything working several months ago, except for 'Insert', which I've now returned to.
That Q2 example had a similar line of code on insert:
That example was NOT using a built in edit form either (sorry, I missed
copying a portion in my code below as I'm not using EditForms).
So basically, from my understanding, in the last line of code above, any method referencing any EditFormItems will not work (up to 'FireCommandEvent). I'd need an alternate method of firing this command when not using EditForms.
Thanks for your patience (I'm new to asp),
I found the demo you are referring to where the grid control is using an external edit form. I have prepared a sample project based on the demo where update and insert works as expected on my end. I am attaching the sample to this post.
To open the external EditForm you need to press a button below the grid. In the ItemCommand event handler the default command is canceled in order to open the form for insert or update.