Some operations on C # DataTable,

Source: Internet
Author: User

Some operations on C # DataTable,

Frequent operations on DATATABLE can achieve the same effect for some operations that do not need to be repeated through SQL.

Method 1:

It is also widely known:

YourDataTable. Columns. Remove ("column name ");

However, this situation is only suitable for removing a small number of columns.

If there are many columns, but I only need one or two columns, method 2 is required.

Method 2:

DataTable dat = YourDataTable. DefaultView. ToTable (false, new string [] {"Name of the column you want", "Name of the column you want "});

 

Add a able:

Operations on DataTable

The easiest thing to think of in a dataTable is to use a for loop for operations, but it is not a last resort because the efficiency is generally not high.

1) Fetch rows

Rowfilter is generally used for row fetch.

DataTable datSource; // data source table

// Filter the table

DataView davTemp = new DataView (datSource, "filter condition", "Sort field", DataViewRowState. Various statuses );

// Assign the filtered table to the new table

DataTable datNew = davTemp. ToTable ();

 

2) retrieve one or more columns of the table

DataTable datSource; // data source table

DataTable datNew = datSource. DefaultView. ToTable (false, new string [] {"column name", "column name ".....});

 

3) copy the value of a row [if the table structure or number of columns are the same]

DataTable datSource;

DataTable datNew;

DatSource. Rows [I]. ItemArray = datNew. Rows [I]. ItemArray;

 

4) What if I want to copy a table with the same number of columns but different column names?

In another way of thinking, since the number of columns is the same, but the column names are different, why not change the column names?

As follows:

DataTable datSource;

DataTable datNew;

DatNew = datSource. Copy ();

DatNew. Columns ["FirstColumn"]. ColumnName = "YourColumnName ";

 

5) Adjust the column position SetOrdinal ();

DataTable dat = new DataTable ();

// Add three columns

Dat. Columns. Add ("col1 ");

Dat. Columns. Add ("col2 ");

Dat. Columns. Add ("col3 ");

// Add a row of data

Dat. Rows. Add (1, 2, 3 );

// Place the third column to the first position

Dat. Columns ["col3"]. SetOrdinal (0 );

Related Article

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.