This is a migrated thread and some comments may be shown as answers.

[Solved] Copy Record to New Record Form

7 Answers 273 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Katie
Top achievements
Rank 1
Katie asked on 16 Jun 2009, 04:21 PM
Hi,

I would like to add a copy button to a record like the edit and delete buttons that would open the add new record form and prepopulate the form items with the data from the record being copied.  This way some of the data can be changed before it is saved to the database and avoid the user having to copy the record and then edit it to make changes.

Any help would be appreciated. Thanks.

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 17 Jun 2009, 04:58 AM
Hi Jeff,

Here is a Codelibrary submission which explains how to copy the row details into an Insert form. Go through it and see if it helps.
Copying the row details into an Insert form

Thanks
Shinu
0
Katie
Top achievements
Rank 1
answered on 17 Jun 2009, 02:39 PM
Great thanks. This is exactly what I am looking for.

I am having some trouble implementing it and i am not sure what I am doing wrong. I have a nested table and it is the nested tables records that I am trying to copy.

Here is my modified code for the Itemcommand event
Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand  
 
        If e.CommandName = "CopyToInsertForm" Then 
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)  
            RadGrid1.MasterTableView.DetailTables(0).IsItemInserted = True 
            RadGrid1.Rebind()  
 
            Dim insertItem As GridEditFormInsertItem = DirectCast(RadGrid1.MasterTableView.DetailTables(0).GetInsertItem(), GridEditFormInsertItem)  
 
            If item Is Nothing Or insertItem Is Nothing Then 
                If item Is Nothing Then 
                    RadAjaxManager1.Alert("item is nothing")  
                End If 
                If insertItem Is Nothing Then 
                    RadAjaxManager1.Alert("insertItem is nothing")  
                End If 
            Else 
                TryCast(insertItem("StartBates").Controls(0), TextBox).Text = item("StartBates").Text  
                TryCast(insertItem("EndBates").Controls(0), TextBox).Text = item("EndBates").Text  
                TryCast(insertItem("Comment").Controls(0), TextBox).Text = item("Comment").Text  
                TryCast(insertItem("BeginStation").Controls(0), TextBox).Text = item("BeginStation").Text  
                TryCast(insertItem("EndStation").Controls(0), TextBox).Text = item("EndStation").Text  
            End If 
 
        End If 
 
    End Sub 
 

I added the test for item and insertItem = nothing for debugging purposes and the insertItem is not being set to the insert form object.

Thanks for your help.
0
Katie
Top achievements
Rank 1
answered on 18 Jun 2009, 01:48 PM
I also noticed that although item is being set to a GridDataItem object, it does not contain the data from the record being copied.
0
Accepted
Yavor
Telerik team
answered on 22 Jun 2009, 06:42 AM
Hello Jeff,

Attached to this message, is a small application, which handles a functionality close to the one that you mentioned.
I hope it gets you started properly.

Best wishes,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Katie
Top achievements
Rank 1
answered on 23 Jun 2009, 03:48 PM
Hi Telerik,  Thanks for the response.

I have decided to abandon using a copy button on each record in favor of adding two buttons on the edit form. One button  'Insert/Update - Remember' saves to the grid and saves the values to session variables.  The other 'Insert/Update - Clear' saves to the grid and clears the session variables. To do this of course I created a FormTemplate so I could add the extra Insert/Update buttons.

Your method works better for this approach, but I am having some difficulties implementing it. When the 'Insert/Update - Remember' button is clicked the Session Variable "CDRemember" is set to "True".  Below is my code. In my tests the session variables do contain values from a previously inserted form (which I verify using the radajaxmanager1.alert command) and the insert form is opening, but the form is blank rather than containing the inserted data.

 
 
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand  
 
        If (e.CommandName = "InitInsert" Or e.CommandName = "Edit") And e.Item.OwnerTableView.DataMember = "CategoryDocuments" Then  
            ' Set Session variable with categoryid of paraent table so that SQLDataSource for subcategories  
            ' will pull correct data on CategoryDocuments nested table  
            Dim parentItem As GridDataItem = CType(e.Item.OwnerTableView.ParentItem, GridDataItem)  
            Session("CategoryID") = parentItem.GetDataKeyValue("CategoryID")  
 
            ' If InitInsert then test for saved values. If True insert saved values  
            ' to insertform.  
            If e.CommandName = "InitInsert" Then  
                If Session("CDRemember") = "True" Then  
                    Dim vals As New Hashtable  
                    vals("StartBates") = Session("CDStartBates")  
                    vals("EndBates") = Session("CDEndBates")  
                    vals("DocumentDate") = Session("CDDocumentDate")  
                    vals("Comment") = Session("CDComment")  
                    vals("HotDocument") = Session("CDHotDocument")  
                    vals("BeginStation") = Session("CDBeginStation")  
                    vals("EndStation") = Session("CDEndStation")  
                    e.Item.OwnerTableView.InsertItem(vals)  
                    RadAjaxManager1.Alert("StartBates:" & vals("StartBates"))  
                End If  
            End If  
        End If  
 
    End Sub  
 

Thanks for your help.
0
Katie
Top achievements
Rank 1
answered on 23 Jun 2009, 04:00 PM
p.s.  If it helps, here is my grid definition.

<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"   
    AutoGenerateColumns="False" DataMember="SubmittalCategories">  
    <MasterTableView CellSpacing="-1" DataMember="SubmittalCategories"   
        DataKeyNames="SubmittalCategoryID, CategoryID" CommandItemDisplay="Top">  
        <DetailTables> 
            <telerik:GridTableView DataMember="CategoryDocuments" runat="server"   
                CellSpacing="-1" DataKeyNames="CatDocID" CommandItemDisplay="Top"   
                EditMode="PopUp">  
                <ParentTableRelation> 
                    <telerik:GridRelationFields DetailKeyField="SubmittalCategoryID" MasterKeyField="SubmittalCategoryID" /> 
                </ParentTableRelation> 
                <Columns> 
                    <telerik:GridBoundColumn DataField="CatDocID" DataType="System.Guid"   
                        HeaderText="CatDocID" ReadOnly="True" SortExpression="CatDocID"   
                        UniqueName="CatDocID" Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataType="System.Guid" HeaderText="SubmittalCategoryID"   
                        ReadOnly="True" SortExpression="SubmittalCategoryID" UniqueName="SubmittalCategoryID"   
                        Visible="False">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn DataField="SubCategoryID" 
                        UniqueName="SubCategoryID" HeaderText="SubCategory" 
                        SortExpression="SubCategoryID">  
                        <ItemTemplate> 
                            <%#Eval("SubCategory")%> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn DataField="StartBates" 
                        UniqueName="StartBates" HeaderText="Start Bates" ForceExtractValue="InEditMode" 
                        SortExpression="StartBates">  
                        <ItemTemplate> 
                            <%#Eval("StartBates")%> 
                        </ItemTemplate>   
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn DataField="EndBates" 
                        UniqueName="EndBates" HeaderText="End Bates" ForceExtractValue="InEditMode" 
                        SortExpression="EndBates">  
                        <ItemTemplate> 
                            <%#Eval("EndBates")%> 
                        </ItemTemplate>   
                    </telerik:GridTemplateColumn> 
                    <telerik:GridDateTimeColumn DataField="DocumentDate" DataType="System.DateTime" 
                        HeaderText="Reference Date" SortExpression="DocumentDate" UniqueName="DocumentDate" 
                        DataFormatString="{0:MM/dd/yyyy}" ForceExtractValue="InEditMode">  
                    </telerik:GridDateTimeColumn> 
                    <telerik:GridTemplateColumn DataField="Comment" 
                        UniqueName="Comment" HeaderText="Comment" ForceExtractValue="InEditMode" 
                        SortExpression="Comment">  
                        <ItemTemplate> 
                            <%# Truncate(Eval("Comment").ToString(),50)%> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridNumericColumn DataField="BeginStation" DataType="System.Int32" 
                        HeaderText="Beg.Sta" UniqueName="BeginStation" ForceExtractValue="InEditMode" 
                        SortExpression="BeginStation">  
                    </telerik:GridNumericColumn> 
                    <telerik:GridNumericColumn DataField="EndStation" Datatype="System.Int32" 
                        HeaderText="End.Sta" UniqueName="EndStation" ForceExtractValue="InEditMode" 
                        SortExpression="EndStation">  
                    </telerik:GridNumericColumn> 
                    <telerik:GridCheckBoxColumn DataField="HotDocument" UniqueName="HotDocument" 
                        HeaderText="HotDocument" SortExpression="HotDocument">  
                    </telerik:GridCheckBoxColumn> 
                    <telerik:GridButtonColumn CommandName="Edit" UniqueName="edit" Text="edit" 
                        ButtonType="ImageButton">  
                        <HeaderStyle Width="40px" /> 
                        <ItemStyle Width="40px" /> 
                    </telerik:GridButtonColumn> 
                    <telerik:GridButtonColumn commandname="Delete" UniqueName="delete" Text="delete" 
                       ButtonType="ImageButton" ConfirmDialogType="RadWindow"   
                       ConfirmText="Are you sure you want to delete?">  
                        <HeaderStyle Width="60px" /> 
                        <ItemStyle Width="60px" /> 
                    </telerik:GridButtonColumn> 
                </Columns> 
                <EditFormSettings PopUpSettings-Modal="true" EditFormType="Template">  
                    <PopUpSettings Modal="true" Width="500px" /> 
                    <FormTemplate> 
                        <table> 
                            <tr> 
                                <td>SubCategory:</td> 
                                <td> 
                                    <telerik:RadComboBox ID="SubCatCB" DataSourceID="SqlDataSourceSubCategories" 
                                        SelectedValue='<%#Bind("SubCategoryID")%>' DataTextField="SubCategory" 
                                        DataValueField="SubCategoryID" Width="300px" runat="server">  
                                    </telerik:RadComboBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>Start Bates:</td> 
                                <td> 
                                    <telerik:RadTextBox ID="StartBatesTB" Text='<%#Bind("StartBates") %>' runat="server">  
                                        <ClientEvents OnLoad="StartBatesLoad" OnBlur="UpdateEndBates" /> 
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>End Bates:</td> 
                                <td> 
                                    <telerik:RadTextBox ID="EndBatesTB" Text='<%#Bind("EndBates") %>' runat="server">  
                                        <ClientEvents OnLoad="EndBatesLoad" /> 
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>Reference Date:</td> 
                                <td> 
                                    <telerik:RadDatePicker ID="RefDateTB" DbSelectedDate='<%#Bind("DocumentDate") %>'  runat="server">  
                                    </telerik:RadDatePicker> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>Comment:</td> 
                                <td> 
                                    <telerik:RadTextBox ID="CommentTB" Text='<%#Bind("Comment")%>' TextMode="MultiLine" Width="400px" Height="75px" runat="server">  
                                    </telerik:RadTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>Beg.Sta:</td> 
                                <td> 
                                    <telerik:RadNumericTextBox ID="BeginStationTB" Text='<%#Bind("BeginStation") %>' NumberFormat-DecimalDigits="0"  runat="server">  
                                    </telerik:RadNumericTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>End.Sta:</td> 
                                <td> 
                                    <telerik:RadNumericTextBox ID="EndStationTB" Text='<%#Bind("EndStation") %>' NumberFormat-DecimalDigits="0" runat="server">  
                                    </telerik:RadNumericTextBox> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td>HotDocument:</td> 
                                <td> 
                                    <asp:CheckBox ID="HotDocumentCB" Checked='<%#returnCheckBoxValue(Eval("HotDocument").ToString) %>' runat="server" /> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td colspan="2">  
                                    <asp:Button ID="btnUpdate" Text='<%# getUpdateButtonTitle(container,1) %>' 
                                        runat="server" CommandName='<%# getUpdateButtonName(container,1) %>' />&nbsp;  
                                    <asp:Button ID="btnUpdate2" Text='<%# getUpdateButtonTitle(container,2) %>' 
                                        runat="server" CommandName='<%# getUpdateButtonName(container,2) %>' />&nbsp;  
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="false" CommandName="Cancel" /> 
                                </td> 
                            </tr> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
            </telerik:GridTableView> 
        </DetailTables> 
    <ExpandCollapseColumn Visible="True" > 
    </ExpandCollapseColumn> 
        <Columns> 
            <telerik:GridBoundColumn DataField="SubmittalID" DataType="System.Guid"   
                HeaderText="SubmittalID" ReadOnly="True" SortExpression="SubmittalID"   
                UniqueName="SubmittalID" Visible="False">  
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataType="System.Guid" HeaderText="SubmittalCategoryID"   
                ReadOnly="True" SortExpression="SubmittalCategoryID" UniqueName="SubmittalCategoryID"   
                Visible="False">  
            </telerik:GridBoundColumn> 
            <telerik:GridTemplateColumn DataField="CategoryID" 
                UniqueName="CategoryID" HeaderText="Category" 
                SortExpression="CategoryID">  
                <EditItemTemplate> 
                    <telerik:RadComboBox ID="RadComboBox1" DataSourceID="SqlDataSourceCategories" 
                        SelectedValue='<%#Bind("CategoryID")%>' DataTextField="Category" 
                        DataValueField="CategoryID" Width="400px" height="300px" runat="server">  
                    </telerik:RadComboBox> 
                </EditItemTemplate>      
                <ItemTemplate> 
                    <%#Eval("Category")%> 
                </ItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridButtonColumn commandname="Delete" UniqueName="delete" Text="delete" 
               ButtonType="ImageButton" ConfirmDialogType="RadWindow"   
               ConfirmText="Are you sure you want to delete?">  
                <HeaderStyle Width="60px" /> 
                <ItemStyle Width="60px" /> 
            </telerik:GridButtonColumn> 
        </Columns> 
    </MasterTableView> 
    <ClientSettings> 
        <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
    </ClientSettings> 
</telerik:RadGrid> 
<asp:SqlDataSource ID="SqlDataSourceCategories" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SGHSubmittalsConnectionString %>"   
    SelectCommand="Select CategoryID, Category From cfgCategories Order By Category">  
</asp:SqlDataSource> 
<asp:SqlDataSource ID="SqlDataSourceSubCategories" runat="server" 
    ConnectionString="<%$ ConnectionStrings:SGHSubmittalsConnectionString %>" 
    SelectCommand="Select SubCategoryID, CategoryID, SubCategory From cfgSubCategories Where CategoryID = @CategoryID">  
    <SelectParameters> 
        <asp:SessionParameter Name="CategoryID" SessionField="CategoryID" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
 
0
Katie
Top achievements
Rank 1
answered on 25 Jun 2009, 10:13 PM
FYI. Found the problem.

I was using the autogenerated command template which contains the 'Add new record' which has the commandName 'InitInsert' and automatically opens the form.

I think the form opens after the ItemCommand routine runs so the 

'e.Item.OwnerTableView.InsertItem(vals)' command was negated by the automatic form open routine which opens an empty form.

I fixed it by setting up a command template, adding link button with a new command name and testing for the new command name in the itemCommand routine.

Added this to the grid markup

<CommandItemTemplate> 
 <asp:LinkButton ID="LinkButton2" runat="server" CommandName="InitInsertCD" ><img style="border:0px" alt="" src="" />Add new Record</asp:LinkButton> 
</CommandItemTemplate>                                     
 
And used this code in the itemCommand routine.
        If e.CommandName = "InitInsertCD" Then 
            If Session("CDRemember") = "True" Then 
                Dim vals As New Hashtable  
                vals("StartBatesTB") = Session("CDStartBates")  
                vals("StartBates") = Session("CDStartBates")  
                vals("EndBates") = Session("CDEndBates")  
                vals("DocumentDate") = Session("CDDocumentDate")  
                vals("Comment") = Session("CDComment")  
                vals("HotDocument") = Session("CDHotDocument")  
                vals("BeginStation") = Session("CDBeginStation")  
                vals("EndStation") = Session("CDEndStation")  
                e.Item.OwnerTableView.InsertItem(vals)  
            Else 
                e.Item.OwnerTableView.InsertItem()  
            End If 
        End If 
 
Now it works as intended.
Tags
Grid
Asked by
Katie
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Katie
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or