DataView RowFilter

DataView class used to represent custom DataTable view.

DataTable and DataView relationship is to follow the well-known design patterns - Document / View mode, which DataTable is a document, and Dataview is a view.

The DataView Table attribute points to the corresponding data sheet, but D ataTable does not store filter information. After the change is both a DataView, DataTable table and nothing will happen.

DataView generally established by DataTable.DefaultView property and creates a subset of the attributes RowStateFilter DataTable by RowFilter properties.

Creating DataView

Method1: DataView with already existing, are likely to be non-empty DataTable object connections

DataView dv; 
dv = new DataView(theDataSet.Tables["Employees"]);

Method2: create a new view, then use Table properties associated with the table

DV = new new DataView DataView (); 
dv.Table theDataSet.Tables = [ "the Employees"]; 
. (DataView constructor allows you to get a DataView object of DataTable if necessary, vice versa in fact, the DefaultView DataTable object attributes. DataView object returns the table).
DataView DV = dt.DefaultView;

 

Filter settings:  

RowFilter is a read-write attribute, the filter table is used to read and set the expression.

public virtual string RowFilter {get; set;}

You can use the column names, any legal combination of logic and digital operators and constants to form expressions.

dv.RowFilter = "Country = 'USA'"; 
dv.RowFilter = "EmployeeID >5 AND Birthdate < #1/31/82#" 
dv.RowFilter = "Description LIKE '*product*'"

dv.RowFilter = "employeeID IN (2,4,5)"

(Where strings must be enclosed in single quotation marks, type the date is to be enclosed with a # sign. Decimal values ​​can be character and scientific notation.)

To access the view in a row, you can use DataRowView class.

Overall, DataRow have up to four states: default, original, current and proposed. These states of DataRowVersion enumerated type provided by RowVersion expression property.

 

Example RowFilter filtered using the following code :( RowDataBound event of GridView)

DataRowView drV = e.Row.DataItem as DataRowView; // get the view of the current row GridView               

. String strPartNumber = drV [ "strPartNumber"] ToString (); // Get strPartNumber field line DRV               

DataView dv = dt_D.DefaultView; // dt_D as has crawled out from the database table              

dv.RowFilter = "strPartNumber = '" + strPartNumber + "'"; // set the filter condition to view dv 'strPartNumber' column is obtained from the line current GridView item number (strPartNumber)            

StringBuilder strFilter = new StringBuilder (); // strFilter dv view data for storage after filtration, and store it to the specified table Table              

strFilter.Append ( "<table id = 'table_info' cellpadding = '3'>"); // id = 'table_info' Table styles defined table, cellpadding = '3' and set the character spacing frame

strFilter.Append("");                

foreach (DataRowView drv in dv) // row after each traversal filter dv view            

{                    

  strFilter.Append("<tr><td>");                    

  strFilter.Append (drv [ "strmono"] ToString ().); // obtain a work order number (strmono)                   

  strFilter.Append("</td><td>");                    

    strFilter.Append ((Convert.ToInt32 (drv [ "nummoqty"]) * Convert.ToInt32 (drv [ "qty"])) ToString ().); // obtains the work order number on the total amount of this material (nummoqty the singular work, qty amount in the BOM for the part number in)                 

  strFilter.Append("</td></tr>");                

}                

strFilter.Append("</table>");               

e.Row.Cells [5] .Text = strFilter.ToString (); // add to the resulting form the specified cell GridView

 

Guess you like

Origin www.cnblogs.com/ljs-13/p/12126451.html