asp.net implements the optional column _ Practical tips in the DataTable

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.