Copy codeThe Code is as follows:
// Data
DataTable dtObject = dt;
// Keep the column
String [] saveColumns = new string [5];
SaveColumns [0] = "X"; // reserved column 1
SaveColumns [1] = "XX"; // reserve column 2
SaveColumns [2] = "XXX ";
SaveColumns [3] = "XXXX ";
SaveColumns [4] = "XXXXX ";
// Remove unnecessary Columns
For (int I = dtObject. columns. count-1; I> = 0; I --) // note that when I ++ is used, the OutOfIndex exception occurs. Because some columns are removed, the column index is reduced, I ++ will exceed the total number of continuously reduced indexes. Note.
{
// Remove the indicator
Bool remove = true;
// Whether it is in the reserved column
For (int j = 0; j <saveColumns. Length; j ++)
{
If (dtObject. Columns [I]. ColumnName = saveColumns [j])
{
// Keep columns not removed
Remove = false; break;
}
}
If (remove)
{
// Remove a column
DtObject. Columns. Remove (dtObject. Columns [I]. ColumnName );
}
}
Return dtObject;