Copy Code code as follows:
Data
DataTable dtobject = DT;
Keep columns
string[] Savecolumns = new STRING[5];
Savecolumns[0] = "X";//Keep column 1
SAVECOLUMNS[1] = "XX";//Reserved Column 2
SAVECOLUMNS[2] = "XXX";
SAVECOLUMNS[3] = "XXXX";
SAVECOLUMNS[4] = "XXXXX";
Remove unwanted columns
for (int i = dtobject.columns.count-1 i >= 0; i--)//Note here, the general habit of using i++ will cause a Outofindex exception, because some columns are removed, the column index is reduced, and the i++ exceeds the total number of indexes that have been decreasing. Attention.
{
Remove indicator
bool remove = true;
Whether in the reserved column
for (int j = 0; J < Savecolumns.length; J + +)
{
if (dtobject.columns[i). ColumnName = = Savecolumns[j])
{
Reserved columns not removed
remove = false; Break
}
}
if (remove)
{
removing columns
DtObject.Columns.Remove (Dtobject.columns[i]. ColumnName);
}
}
return dtobject;