I. Sorting
1. Get the default view of DataTable
2. Set the sorting expression for the view
3. Replace the able with the new DataTable exported from the sorted view.
(Asc can be omitted in ascending order. Separate multiple columns with commas)
DataView dv = dt. DefaultView;
Dv. Sort = "id Asc, name Desc ";
Dt = dv. ToTable ();
Ii. Search
1. Set the query string
2. Use the Select method to obtain an array of all data row objects that meet the conditions.
(Multiple query conditions are separated by and. like % is used for fuzzy query)
DataRow [] matches = dt. Select ("(id> = 20) and (title = 'conferencing ') and (yearstr like' % 2007% ')");
String strName = matches [0] ["name"]. ToString ();
You can also bind the retrieved new table to the GridView using the following method.
DataView dv = dt. DefaultView;
Dv. RowFilter = "id> = 2 ";
This. GridView1.DataSource = dv;
This. GridView1.DataBind ();
Iii. Merge
Assume that there are 2 DataTable: Dt1, Dt2. Same table structure
This method can be used after Dt2 is connected to Dt1.
Dt1.Merge (dt2 );
4. Paging
PagedDataSource PPS = new PagedDataSource ();
PPS. AllowPaging = true;
Pds. DataSource = dvIntegralExpense;
PPS. AllowPaging = true;
PPS. PageSize = pager. PageSize;
PPS. CurrentPageIndex = pager. PageIndex;
RptIntegralExpense. DataSource = PPS;
RptIntegralExpense. DataBind ();
V:
When we use SQL Server databases, we can easily calculate the relevant results through Sum, Aver, Count, and so on.
If so, what about DataSet (DataTable) that has retrieved the data? Especially through Web Service
DataSet, but there is no way to go back and modify the Select statement to obtain these statistics. In DataSe
Does t/DataTable support statistics? The answer is yes.
In MSDN, there is a statistical method recommended by MS, which is to calculate the sum of Data row by row.
Equals to none (maybe this method is only used for the DataGrid to calculate the subtotal), because this method uses the semi ri
The ItemDataBind event of d is used to accumulate data, which is no different from the code statistics written manually.
This article introduces a simple method to easily obtain records in a able without having to calculate records one by one.
Statistical results. This simple method is to call the powerful DataTable function Compute.
1. Call description (C # is used as an example, the same below ):
Public object Compute (string strExpression, string strFilter)
Parameters:
StrExpression: expression string to be calculated. It is basically similar to the statistical expression in SQL Server.
StrFilter: A Statistical filter string. Only records that meet this filter condition are counted.
Ii. Call example:
In the following example, assume a product sales table that describes the actual sales records of various promoters in a mall, including fields
Is: Name, gender (Sex, 0 is female, 1 is male), Birthday (Birthday), sales product code (ProID
), Sales Quantity, and Price ).
1. count the number of salespersons whose gender is female:
Table. Compute ("Count (*)", "Sex = 0 ");
2. Count all sales staff older than 20
Table. Compute ("Count (*)", "Birthday <'" + today); // today is the date string of today.
3. Calculate the average prices of products sold
Table. Compute ("Aver (Price)", "true ");
4. count the number of products sold with the product code 1:
Table. Compute ("Sum (Quantity)", "ProID = 1 ");
5. Total sales of all products:
To calculate the total sales amount, because the table does not contain the sales amount of a certain promoter of a product, but we can
Obtained Through Quantity * Price. For example:
Table. Compute ("Sum (Quantity * Price)", "true ");
Here is a problem: the DataTable statistics function is not strong in SqlServer, this statistics is incorrect, because Compu
Te statistics do not support data such as Sum (Quantity * Price. What should we do?
For Statistics of such complex data, we can create a new field in the DataTable, such as Amount.
At the same time, set the Expression of this field to Quantity * Price, so that we can use the statistical function:
Table. Compute ("Sum (Amount)", "true ");