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

Copy radgrid rows

1 Answer 276 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JOSE MANUEL PÉREZ RAMÍREZ
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);
 this.radGridViewEtiqueta.GridElement.BeginUpdate();
for (int i = 0; i < selectedRows.Length; i++){
    this.radGridViewEtiqueta.Rows.AddNew((GridViewDataRowInfo)selectedRows[i]);
}
 this.radGridViewEtiqueta.GridElement.EndUpdate();

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

1 Answer, 1 is accepted

Sort by
0
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; 
        } 
 
        destinationGrid.Rows.Add(values); 
    } 
 
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; 
 
        destinationGrid.Columns.Add(col); 
    } 


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.
Tags
GridView
Asked by
JOSE MANUEL PÉREZ RAMÍREZ
Top achievements
Rank 1
Answers by
Martin Vasilev
Telerik team
Share this question
or