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

Copy a Record

8 Answers 420 Views
Grid
This is a migrated thread and some comments may be shown as answers.
carlos
Top achievements
Rank 1
carlos asked on 09 Jan 2009, 10:45 AM
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

8 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 12 Jan 2009, 08:12 AM
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.
0
Shinu
Top achievements
Rank 2
answered on 12 Jan 2009, 09:03 AM
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.
0
carlos
Top achievements
Rank 1
answered on 12 Jan 2009, 03:47 PM
Hy Friends, Can I have the above code in VB language?

Thanks
0
carlos
Top achievements
Rank 1
answered on 12 Jan 2009, 04:53 PM
How can I triger an RadGrid1_ItemCommand event?

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

thanks

0
Accepted
Shinu
Top achievements
Rank 2
answered on 13 Jan 2009, 04:32 AM
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.
0
carlos
Top achievements
Rank 1
answered on 13 Jan 2009, 07:28 PM
Perfect! Thanks
0
Vijay
Top achievements
Rank 1
answered on 17 Jun 2016, 07:49 PM
Can the cloning of the GridDataItem be done on Client Side? 
0
Eyup
Telerik team
answered on 22 Jun 2016, 10:37 AM
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.
Tags
Grid
Asked by
carlos
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Shinu
Top achievements
Rank 2
carlos
Top achievements
Rank 1
Vijay
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or