As far as the Multi-Add. Here is the code that makes that work. In combination with the ScrapeAllRows code, this makes for a RadGrid that you can multi-add, and multi-edit, without losing changes made so far.
In the Grid, add a button in the CommandItemTemplate that calls your custom Add command. (AddNewThing in this example).
| <CommandItemTemplate> |
| <table width="100%"> |
| <tr> |
| <td> |
| <asp:LinkButton ID="LinkButton2" runat="server" CommandName="AddNewThing" Text="Add Thing" /> |
| </td> |
| </tr> |
| </table> |
| </CommandItemTemplate> |
In the code behind, you need to fake out the Add. We're actually going to add the item to the collection, and save it to the database, and then reload the grid with that item in edit mode, so we actually skip the whole "ItemInserted" event. The grid does some strange binding things for inserted rows, and will only allow one row in insert mode at a time. This code works around that.
Remember, I am binding my grid to a Collection, NOT a DataSet, and I am NOT using a Object/SQL/Etc.DataSource of any sort. All the binding is handled in the code.
| Private Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource |
| Me.RadGrid1.DataSource = ThingController.Things |
| End Sub |
So, first, we need to handle the AddNewThing command, like this:
| Private Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand |
| If e.CommandName = "AddNewThing" Then |
| ScrapeAllEditRows() |
| Dim aThing As New Thing |
| aThing.Guid = Guid.NewGuid() |
| aThing.EstimatedBirthMonth = Date.Now.ToLongTimeString |
| ThingController.Things.Add(aThing) |
| _newThing = aThing |
| RadGrid1.Rebind() |
| End If |
| End Sub |
Next, notice how we set the class variable _newThing equal to the new aThing that we just created? When the Grid rebinds, we can catch the PreRender event and check to see if there is a Thing in the _newThing variable, if there is, then we must have just added one, and we need to make it editable and Rebind the grid again (Yes, rebind again, it's weird that way.), like this:
| Private Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender |
| If _newThing IsNot Nothing Then |
| For Each item As GridDataItem In RadGrid1.MasterTableView.Items |
| Dim aThing As CodeForTelerik.Thing = DirectCast(item.DataItem, Thing) |
| If aThing.Guid.Equals(_newThing.Guid) Then |
| If Not item.IsInEditMode Then |
| 'clear the newdeal reference and set the row into edit mode |
| _newThing = Nothing |
| item.Edit = True |
| RadGrid1.MasterTableView.Rebind() |
| Exit Sub |
| End If |
| End If |
| Next |
| End If |
| End Sub |
As with the ScrapeAllRows code, we are messing with the in-memory collection, so we are not persisting any of these changes into the database until the user tells us to. If the user Cancels or Refreshes, the collection/object is reloaded from the database and the new item is discarded.
I hope that helps. Please post to this thread if you have any questions or if you have a better suggestion (please, I wish I knew a better way to do this).