Click here to Skip to main content
11,638,881 members (69,147 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi everyone!

Part of my project is to give the user an option to clone/duplicate a row to the bottom "newrow" incase they want to make 1 or 2 cell edits. This is what I have so far in accomplishing this mission.

try
            {
                highlightBottomRow();
                setDuplicateRowValues();
                setNewRowCellValues();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

setDuplicateRowValues is supposed to copy the selected row cells to a variable (1 variable per cell). setNewRowCellValues is supposed to set the values of the cells in the newrow(editing row) the values of what the variables contain. Is there a more efficient way to do this (i.e. with the Clone method)? The first column is an ID column and we want to increment each new addition (no matter if it's a duplicated row or a row created brand new and keep the first column read-only). I have 25 columns and want this to be as performance efficient as possible.

Thanks everyone!
Posted 13-Feb-13 3:56am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

DataRow's have an ItemArray property that represent all the field values as an object[].

DataRow existingrow = //Your existing row;
var newrow = tbl.NewRow();
newrow.ItemArray = existingrow.ItemArray;

And then just increment the ID if you're DataTable's schema isn't configured to do so. This gives you a bit of protection against future column changes and data type consistency. Should be faster too - haven't benchmarked it myself yet though.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 9,061
1 Sergey Alexandrovich Kryukov 8,773
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,494
4 F-ES Sitecore 2,438


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100