Click here to Skip to main content
13,000,851 members (62,457 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In my web application assign DB values to a Datatable and it assigned to another datatable object. But when we edit this same change occur in copied datatable object.

Datatable dt=new DaTatable()
Datatable dtcopy=new DaTatable()
dt.columns["NameEng"].ColumnName = "Name";

Now in datatble dt's column 'NameEng' change to 'Name .
But same change occur in dtcopy. What is the reason and how can avoid it.
Posted 26-Nov-12 2:11am
Updated 26-Nov-12 2:25am

1 solution

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

Solution 1

This is because both dtcopy and dt are referencing the same object.
Use DataTable.Copy Method[^] to copy from one datatable to another.

Instead of

dtcopy = dt.Copy();

to copy the data table.

If you want only the structure of datatable and not the data you can use DataTable.Clone Method[^]

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

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 26 Nov 2012
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