NET DataTable Quick Customization----Expression Property expressions

Source: Internet
Author: User

This article is intended to discuss customizing columns using the DataColumn.Expression property, bypassing the master.

Here are just two classic scenes, and more features to discover!

Scenario One:

There are two columns in the DataTable for each commodity [price] and [Num] quantity, which now requires the total value of each commodity:

Method one (not recommended):

Add a column [value], traverse the DataTable with [Price]*[num] for each row to get value, then assign the column [value], the code is as follows:

Dt. Columns.Add ("value"); // Increase the value of the new column value storage foreach  in dt. Rows)// Traverse calculates each commodity and stores it in column [value] {    dr["value"] = dr["Price"]*dr[" num "];}
Copy Code

You can then perform regular binding display operations.

Method Two (recommended):

Add a column [Value] and use the column's expression property to customize this column with the following code:

Dt. Columns.Add ("value");d T. columns["value""price* num";
Copy Code

This is good, then you can do a regular binding display operation, because expression expressions are deferred execution, that is, binding is executed, so there is no binding, the content of the DataTable will be seen unchanged, If you want to get the table under the action of this expression, use the method: ToTable ();

As follows:

DataTable newtable = dt. Defaultview.totable ();

This only describes the use of mathematical expressions, similar to the addition of subtraction can be implemented, if you want to concatenate string what to do? Please see the following example:

Scenario Two:

For example: Two columns in the DataTable are [name] department name and [Departid] Department ID, now need a department and ID to join the column to show for example: Human Resources department-010 how to do? Here's how to use the string join expression, see Code:

Dt. columns["nameandid" "name+ '-' +departid";

Here the "+" as the role of the connection string, no longer do not traverse the method to achieve similar functions, of course expression can play more than this function, see: http://msdn.microsoft.com/zh-cn/library/ System.data.datacolumn.expression.aspx

More than one column can be customized, we can also manipulate the DataTable's rows in the same way as SQL operations database, with the DataTable.DefaultView.RowFilter property. Next time to introduce it is interested in can search by themselves.

Source: http://blog.csdn.net/jacky4955/article/details/7282678

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.