Click here to Skip to main content
12,946,164 members (52,739 online)
Rate this:
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.

            catch (Exception ex)

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
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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,205
Maciej Los 2,453
Jochen Arndt 1,935
ppolymorphe 1,795

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2017
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