Manipulating data in a DataTable

Source: Internet
Author: User

In a project, you often encounter such problems. After you have used SQL to select the required data from the database into a DataTable, you need to do some processing before you can use it to display on the page.

Before these problems, are encountered a Baidu, no system to deal with these problems, write a document today to deal with this problem

Let's start with a few simple ones.

To add columns and rows:

(since it is tidy, write a full point, three methods)

#regionMethod One:DataTable Tbldatas=NewDataTable ("Datas");//Table nameDataColumn DC =NULL; DC= TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32")); dc. AutoIncrement=true;//automatically adddc. AutoIncrementSeed =1;//starting at 1dc. AutoIncrementStep =1;//Step size is 1dc. AllowDBNull =false;//not allowed to be emptyDC = TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String")); DC= TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String")); DC= TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String")); //DataRow NewRow; NewRow=Tbldatas.newrow (); newrow["Product"] ="This place is the value of the cell."; newrow["Version"] ="2.0"; newrow["Description"] ="This place is the value of the cell."; TblDatas.Rows.Add (NewRow); NewRow=Tbldatas.newrow (); newrow["Product"] ="This place is the value of the cell."; newrow["Version"] ="3.0"; newrow["Description"] ="This place is the value of the cell."; TblDatas.Rows.Add (newRow);#endregion

After reading a method, you may ask the DC=TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String")); Why use dc= this thing Ah TblDatas.Columns.Add ("Product", Type.GetType ("System.String")  ); You can do it directly with one. Indeed, it is also possible to use dc= to conveniently set properties for this column. For example, I want to set this column cannot be empty I can DC. AllowDBNull =false; Just like the first column.

#regionMethod Two:DataTable Tbldatas=NewDataTable ("Datas"); TBLDATAS.COLUMNS.ADD ("ID", Type.GetType ("System.Int32")); tbldatas.columns[0]. AutoIncrement =true; tbldatas.columns[0]. AutoIncrementSeed =1; tbldatas.columns[0]. AutoIncrementStep =1; TBLDATAS.COLUMNS.ADD ("Product", Type.GetType ("System.String")); TBLDATAS.COLUMNS.ADD ("Version", Type.GetType ("System.String")); TBLDATAS.COLUMNS.ADD ("Description", Type.GetType ("System.String")); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); TBLDATAS.ROWS.ADD (newobject[] {NULL,"a","b","C" }); #endregion

This method does not use the Set attribute used by dc= to tbldatas.columns[0]. AutoIncrement =true; So to set, personally feel more troublesome. This is the time to assign values to the columns of the table, adding a row of data in order. And the first method is a column of auxiliary, then it is more troublesome. Dt. Rows.Add (new object[] {null, "Tang", "W", "25", "50"}); Here the first value is assigned NULL because this is an auto-add column so you want to assign a value of NULL

#regionMethod Three:DataTable Table=NewDataTable ();//Create the first column of a tableDataColumn Pricecolumn =NewDataColumn (); Pricecolumn.datatype= System.Type.GetType ("System.Decimal");//the data type of the columnPricecolumn.columnname =" Price";//the name of the columnPricecolumn.defaultvalue = -;//The default value for this column//Create the second column of a tableDataColumn Taxcolumn =NewDataColumn (); Taxcolumn.datatype= System.Type.GetType ("System.Decimal"); Taxcolumn.columnname=" Tax";//Column NameTaxcolumn.expression ="Price * 0.0862";//set the column to an expression that evaluates the values in a column or creates an aggregate column//Create the third column of a tableDataColumn Totalcolumn =NewDataColumn (); Totalcolumn.datatype= System.Type.GetType ("System.Decimal"); Totalcolumn.columnname=" Total"; Totalcolumn.expression="Price + Tax";//The column's expression is the first column and the second column worth and//Add all columns to the tabletable. Columns.Add (Pricecolumn); Table. Columns.Add (Taxcolumn); Table. Columns.Add (Totalcolumn); //Create a rowDataRow row =table. NewRow (); Table. Rows.Add (row);//Add this line to the table

This method is relatively small, but also useful, for example, I choose a field from the DB, when I bind to the page, I add a few fields (percentage, sum) to add a column in the table totalcolumn.expression =" Price + tax"; Use this method to calculate.

The Select method is of course used to manipulate the data in the table. Let's talk about this method.

Select method

First select this method, a total of four overloaded functions

1:select ()

2:select (String filterexpression)

3:select (String filterexpression, string sort)

4:select (String filterexpression,string Sort, dataviewrowstate record states).

To put it bluntly, it is no big problem to use this method only to understand what these parameters mean. Then just one.

1:select ()

This has no parameters, it is not to say. is to select the data in the DataTable. It is important to note, however, that the Select method selects an array of DataRow objects. The use of this parameter is relatively small.

2:select (string filterexpression)

This filterexpression is an expression. This is going to be a study. Expressions support "and, like, or"

Like what

Select ("id>= ' 3 ' and name= ' hello '); (SELECT ID This field is greater than or equal to 3 and name equals "Hello" data)

Select ("id>= ' 3 ' or id= ' 1 '"); (Choose data with ID greater than or equal to 3 or ID equal to 1)

Select ("Name like '%hello% '"); (select name with Hello data)

Manipulating data in a DataTable

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.