Home Forums WinForms controls Xceed Grid for WinForms How to copy a row Reply To: How to copy a row

User (Old forums)
Member
Post count: 23064

I don’t really know an alternative method.

Here’s the best I can come up with:

// START CODE
Xceed.Grid.DataRow existingRow = …; // the existing row you want to copy
Xceed.Grid.DataRow newRow = myGrid.DataRows.AddNew();

Xceed.Grid.Collections.CellList existingCells = existingRow.Cells;
Xceed.Grid.Collections.CellList newCells = newRow.Cells;
int count = newCells.Count;

for(int i=0; i<count; i++) newCells[i].Value = existingCells[i].Value;
// END CODE

Is your current method too slow for your application? Don’t try to optimize if it goes fast enough. If you need to do this a lot, like inside a loop, and this method is too slow, it might be better to work on the underlying DataSource directly. If the DataSource of your grid is a DataTable, it might be better to do it like this:

// START CODE
Xceed.Grid.DataRow existingRow = …; // the existing row you want to copy

System.Data.DataTable table = …; // the underlying DataTable
System.Data.DataRowCollection rows = table.Rows;
object[] existingRowData = rows[existingRow.Index].ItemArray;

myGrid.BeginInit();
for(….) rows.Add(existingRowData);
myGrid.EndInit();
// END CODE

The code above has not been tested yet, so it might even be slower, I don’t know.

Imported from legacy forums. Posted by Tommy (had 3083 views)