Copy radgrid rows

2 posts, 0 answers
    22 posts
    Member since:
    Jan 2008

    Posted 25 Sep 2008 Link to this post

    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?

  2. Martin Vasilev
    Martin Vasilev avatar
    1061 posts

    Posted 26 Sep 2008 Link to this post

    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.
Back to Top