Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
A datatable has several fields, i.e. field1, field2, field3, field9, field10, field88...

string strData = "field1, field2, field3, ...";


Question:
looping through the columns of the datatable, I would like to remove the columns in strData.
So the datatable ends up with the following columns:
field9, field10, field88...

HOw is the above done in C# please?

Thanks
Posted
Updated 30-Jan-12 3:38am
v2

use this
dt.Columns.Remove

where dt is DataTable

Thanks
--RA
 
Share this answer
 
Comments
arkiboys 30-Jan-12 11:27am    
Thanks
Hi,

do you want to remove this columns from database table or from DataTable object?

in first case you have to use SMO:
http://msdn.microsoft.com/en-us/library/ms204453.aspx[^]

in second case just split this string into array and loop through this array :

C#
DataTable t = ...
foreach(string col in cols)
{
   t.Columns.Remove(col);
}


cols variable represents string array of column names
 
Share this answer
 
Comments
arkiboys 30-Jan-12 11:27am    
Thank y ou

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



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