Copy a Record

9 posts, 1 answers
  1. carlos
    carlos avatar
    16 posts
    Member since:
    Dec 2008

    Posted 09 Jan 2009 Link to this post

    Hy fellas,

    I need to add a button to a gridview like an insert or delete button. This button will be called "Copy and Paste" and whenever I click this button the same line of the gridview (with the same data) will be added as a new record to the gridview. How can I achive this?

    Thanks a lot
  2. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 12 Jan 2009 Link to this post

    Hello carlos,

    Thank you for contacting us and for your question.
    One possible approach in this case would be to use a command Item template, to raise a custom command. From the code-behind, in the Item command event handler, you can add the information from the selected item directly to the database, and then rebind the grid. This will ensure that the item is added to the grid contents.
    I hope this suggestion gets you started properly.

    Kind regards,
    Yavor
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Jan 2009 Link to this post

    Hi Carlos,

    I hope you want the button inline with GriddataItem. In that case you can use a GridButtonColumn and check the commandname wheher it is same like set for the colummn and in "Item_Command" event and by using database object it is possible to create a new duplicate row for the row which is clicked. Please try the following code snippet and see if it fits your scenario.

    CS:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        if (e.CommandName == "CopyPaste"
        { 
            GridDataItem gd = (GridDataItem)e.Item; 
            string str = gd["EmployeeID"].Text.ToString(); 
            DataSet employeesData = new DataSet(); 
            String ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; 
            SqlConnection conn = new SqlConnection(ConnString); 
            SqlDataAdapter adapter = new SqlDataAdapter(); 
            adapter.SelectCommand = new SqlCommand("SELECT * FROM Employees", conn); 
            adapter.Fill(employeesData, "Emp"); 
     
            DataTable dt = employeesData.Tables["Emp"]; 
     
            DataRow[] rows = dt.Select("EmployeeID=" + str); 
            DataRow row = dt.NewRow(); 
            //row[0] = number;   set the Primary key to desired, in this case I have used Autoincrement Column 
            for (int i = 1; i < dt.Columns.Count; i++) 
            { 
                row[i] = rows[0][i]; 
            } 
            dt.Rows.Add(row); 
            SqlCommandBuilder cmdb = new SqlCommandBuilder(adapter); 
            adapter.Update(employeesData, "Emp"); 
        } 

    Thanks,
    Shinu.
  5. carlos
    carlos avatar
    16 posts
    Member since:
    Dec 2008

    Posted 12 Jan 2009 Link to this post

    Hy Friends, Can I have the above code in VB language?

    Thanks
  6. carlos
    carlos avatar
    16 posts
    Member since:
    Dec 2008

    Posted 12 Jan 2009 Link to this post

    How can I triger an RadGrid1_ItemCommand event?

    Do I have to add an Hyperlink column or an select column?

    thanks

  7. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 12 Jan 2009 Link to this post

    Hi Carlos,

    By using GridButtonColumn, you can achieve this. Set the CommandName of the GridButtonColumn as "CopyPaste" and thus you can check the CommandName in server side whether it is same. Check out the following example.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" onitemcommand="RadGrid1_ItemCommand" AllowSorting="True"
     
    <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
        <Columns> 
            <telerik:GridButtonColumn ButtonType="PushButton" CommandName="CopyPaste" UniqueName="insertButton" Text="Copy & Paste"
            </telerik:GridButtonColumn> 
            <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32"  
                HeaderText="EmployeeID" SortExpression="EmployeeID" UniqueName="EmployeeID"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName"  
                SortExpression="LastName" UniqueName="LastName"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName"  
                SortExpression="FirstName" UniqueName="FirstName"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="BirthDate" DataType="System.DateTime"  
                HeaderText="BirthDate" SortExpression="BirthDate" UniqueName="BirthDate"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="City" HeaderText="City"  
                SortExpression="City" UniqueName="City"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Region" HeaderText="Region"  
                SortExpression="Region" UniqueName="Region"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="PostalCode" HeaderText="PostalCode"  
                SortExpression="PostalCode" UniqueName="PostalCode"
            </telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField="Country" HeaderText="Country"  
                SortExpression="Country" UniqueName="Country"
            </telerik:GridBoundColumn> 
        </Columns> 
    </MasterTableView> 
     
    </telerik:RadGrid> 


    VB:
    Protected Sub RadGrid1_ItemCommand(ByVal source As ObjectByVal e As GridCommandEventArgs) 
         If e.CommandName = "CopyPaste" Then 
             Dim gd As GridDataItem = DirectCast(e.Item, GridDataItem) 
             Dim str As String = gd("EmployeeID").Text.ToString() 
             Dim employeesData As New DataSet() 
             Dim ConnString As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString 
             Dim conn As New SqlConnection(ConnString) 
             Dim adapter As New SqlDataAdapter() 
             adapter.SelectCommand = New SqlCommand("SELECT * FROM Employees", conn) 
             adapter.Fill(employeesData, "Emp"
             
             Dim dt As DataTable = employeesData.Tables("Emp")       
             Dim rows As DataRow() = dt.[Select]("EmployeeID=" & str) 
             Dim row As DataRow = dt.NewRow() 
             'row[0] = number; set the Primary key to desired 
             For i As Integer = 1 To dt.Columns.Count - 1 
                 row(i) = rows(0)(i) 
             Next 
             dt.Rows.Add(row) 
             Dim cmdb As New SqlCommandBuilder(adapter) 
             adapter.Update(employeesData, "Emp"
             RadGrid1.Rebind() 
         End If 
    End Sub 


    Thanks,
    Shinu.
  8. carlos
    carlos avatar
    16 posts
    Member since:
    Dec 2008

    Posted 13 Jan 2009 Link to this post

    Perfect! Thanks
  9. Vijay
    Vijay avatar
    1 posts
    Member since:
    Jun 2016

    Posted 17 Jun in reply to Shinu Link to this post

    Can the cloning of the GridDataItem be done on Client Side? 
  10. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 22 Jun Link to this post

    Hi Vijay,

    Generally, you can achieve this requirement only on server-side. Check the attached web site sample for a reference.

    There is also the following property for standard columns:
    Copy Code
    Copy Code
    <telerik:GridBoundColumn ... DefaultInsertValue="Success">

    Batch editing mode, however, is different than other modes. You can find a detailed explanation in the following section:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-batch-edit-mode


    With this mode, you can add a new record without posting back to the server:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/edit-mode/batch-editing/client-side-api


    You can use the following event handler to intercept the cell and modify its value:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/client-side-programming/events/onbatcheditopened

    I hope this will prove helpful.

    Regards,
    Eyup
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017