We help IT Professionals succeed at work.

Add and Remove columns from a datagrid

narmi2
narmi2 asked
on
1,299 Views
Last Modified: 2008-08-08
Hi

Is it possible to somehow add and remove columns from a dataset, datatable, or dataview without another round trip to the sql server?

Basically I would like to allow the user to add or remove columns from the datagrid once it has been bound!

As the sql query is large and slow, I do not want it to go check the server each time a column is added or removed

Thanks
Comment
Watch Question

So you want the new column to be added to the table, locally.. and not in the original database. Am I right?

-Baan

Author

Commented:
Not exactly!

Yes it should add and remove columns, but it should only be able to add columns which have been removed!

Author

Commented:
and yes it must not affect the database.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks!

So now I have been able to copy a datatable, which perfect!

I can remove a column from one of those datatables without effecting the other datatable, which is also perfect!

But I still cannot copy a column from 1 datatable to another?

I have 2 datatables

dtOriginal
dtCopy

dtCopy is a copy of dtOriginal

e.g.

dtCopy = dtOriginal.Copy

I know how to remove a column from dtCopy e.g.

dtCopy.Columns.Remove("First Name")

But now once this column has been removed, later I might want to add it back to dtCopy again.

But each time I try it says that that column name already exists in another datatable.  This is true because it exists in dtOriginal.

Is it not possible to copy 1 column from 1 datatable into another datatable?

Or is it only possible to copy all of dtOriginal back into dtCopy and removed when is not needed?

Thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What are you trying to copy, the column, or the column + data?  If you are just trying to copy the column, just create a new one with the exact same settings as the old one.

Bob

Author

Commented:
Unfortunately I need to copy the column + data.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.