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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13287 |

    Hi all,

    what is the fastest way to copy an existing datarow? I did not find any existing methods for this, so currently I add a new row and copy all cells from the source row, but I guess there is a faster method…?

    Imported from legacy forums. Posted by Kai (had 1825 views)

    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)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.