Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 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.

C#
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

1 solution

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.
 
Share this answer
 

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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900