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

Copy radgrid rows

1 Answer 406 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
JOSE MANUEL PÉREZ RAMÍREZ asked on 25 Sep 2008, 11:50 AM
I have two radgrid.
First is binding to object  (radGridAcuse) but second has not binding(radGridEtiqueta).
Both have the same column definitiĆ³n.
I select some rows from first, and click a button to copy.

GridViewRowInfo[] selectedRows = new GridViewRowInfo[this.radGridViewAcuse.SelectedRows.Count];this.radGridViewAcuse.SelectedRows.CopyTo(selectedRows, 0);
for (int i = 0; i < selectedRows.Length; i++){

this code raise exception.
Is possible add rows to second Grid?
Do both Grid have same Columns?
Is possible drag and drop?

1 Answer, 1 is accepted

Sort by
Martin Vasilev
Telerik team
answered on 26 Sep 2008, 06:22 PM
Hi Jose,

Thank you for writing.

Direct copying the SelectedRows collection to other RadGridView is not a good practice, because the grid virtualization from the first instance will add additional code to get every cell object with its value in the second instance. Also, since the GridViewDataRowInfo object is a reference type you have to implement a deep copy, if you do not want changes in first grid to affect the second one.

Nevertheless, I can suggest implementing the copy method by using only the cells values. You have to create the same column schema for the destination grid, and implement RadGridView.Rows.Add() method. Please, review the code-block below as example:
private void Form1_Load(object sender, EventArgs e) 
    this.CopyColumnSchema(this.radGridView1, this.radGridView2); 
private void radButton1_Click(object sender, EventArgs e) 
    this.CopySelectedRows(this.radGridView1, this.radGridView2); 
private void CopySelectedRows(RadGridView sourceGrid, RadGridView destinationGrid) 
    for (int i = 0; i < sourceGrid.SelectedRows.Count; i++) 
        object[] values = new object[sourceGrid.ColumnCount]; 
        for (int c = 0; c < sourceGrid.SelectedRows[i].Cells.Count; c++) 
            values[c] = sourceGrid.SelectedRows[i].Cells[c].Value; 
private void CopyColumnSchema(RadGridView sourceGrid, RadGridView destinationGrid) 
    for (int i = 0; i < sourceGrid.ColumnCount; i++) 
        GridViewDataColumn col = new GridViewDataColumn(); 
        col.HeaderText = sourceGrid.Columns[i].HeaderText; 
        col.UniqueName = sourceGrid.Columns[i].UniqueName; 
        col.DataType = sourceGrid.Columns[i].DataType; 
        col.Width = sourceGrid.Columns[i].Width; 

I hope this will fit to your scenario. Feel free to extend this example according to your needs.

All the best,
Martin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Asked by
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Share this question