Some easily confusing concepts in DataTable, DataView and DataGrid

xiaoxiao2021-03-06  56

Some easily confusing concepts in DataTable, DataView and DataGrid

First, DataTable DataTable indicates a table in the memory, which is completely independent in memory, which contains all the information of this table. DataTable can be from a table that is formed from the database through the connection, once this DataTable can be opened independently with the data source, it can also be created by the programself by the program. A table. ◆ The Datacolumn is a two-dimensional structure consisting of rows and columns. The structure of the table is composed of a collection of DataColumn objects. The DataColumn object collection can be obtained from the DataTable.Columns property that determines the schema in the table by defining the data type of each column. Defining the structure of the table can generate DATAROW according to the structure, use the DataTable.newrow () method to generate a new line of this DataTable structure. A DATATABLE is composed of DataRow's collection, and the collection of DataRow can be accessed by the DataTable.Rows property. DataTable can also create some columns through expressions with existing Expression properties. 1. Create a calculated column such as: There is a table structure, there is a collection of Datacolumn in the table, with a column called Unitprice, you can create a new Datacolumn, set the columnName, set this column, Datacolumn.expression = "UnitPrice * 0.086", the value of this column is calculated by column for Unitprice. When creating an expression, use a columnName property to reference columns. 2, the second use is to create a polymerization column aggregation usually along the relationship (see the DataRelation section below related to the relationship), if the ORDER table is known as the subtaby table named Detail, two tables pass ORDER. ORDERID and DETAIL Two columns to establish a relationship DataRelation object name "Order2Detail", you can create a gale column in the primary table ORDER, calculate the price of all Items included in the Detail table: Datacolumn.Expression = "SUM (Order2Detail)", Child (ORDER2DETAIL) represents the child table that is linked by the relational order2detail, and Child (ORDER2DETAIL) .price represents the PRICE column of the sub-table. ◆ The DATAROW DATAROW object does not use the constructor used in the code, typically from a new DataRow object with a new () method from a DATATABLE with a certain structure. A DATAROW is different from whether it is independent or a DATATABLE, whether it is modified, whether it is different from DataTable deletion, etc., is disclosed by the DATAROW.ROWSTATE attribute, as shown in the following table:

Member name

Description

Added The row has been added to DataRowCollection, and AcceptChanges has not been called. DELETED This line has been deleted through the DELETE method of DATAROW. DELETED This line has been deleted through the DELETE method of DATAROW. Detached This line has been created, but does not belong to any DataRowCollection. DataRow is immediately in this case: Before being created, it is added to the collection; or remove it from the collection. Modified has been modified, and AcceptChanges has not been called. Unchanged This line has not changed since the last call AcceptChanges. A DATAROW object is just created, its status is detached, is an isolated one existence, so after the DataRow is created, the data is filled with the data in DataRow and add this DataRow to DataTable. After DATAROW is added to DataTable, the status of this DATAROW is converted to Added. When this DATAROW is modified, this DATAROW status is converted to modified. When DataRow.delete () method is deleted, the DATAROW status will be turned to deleted, but this line still exists in the DataTable, but the state changes, at this time Use DataTable.Rows.count to view the number of rows, before the deletion is the same. This DATAROW is only removed from the DataTable and the status is also removed from the DataTable.Remove (DataRow) method. Once the DataTable.AcceptChanges () method is called, all rows will do different processing, add, modified, unchange according to different states, and the DELETED's line will be removed from the DataTable, and the status of all rows are set. To unchanged. When DataTable is formed from a DataAdapter.Fill (DataSet, DataTable) method, the Fill () method will automatically call the AcceptChanges () method, set the DataTable's row status to unchanged. And, if the DataTable specified in the Fill method does not exist when the Dataset to be filled, a DataTable with the same structure as the data source table is generated and populated. ◆ DataRelation represents the parent / sub-relation between the two DataTable objects. You can class more than the relationship between the tables in the database, the parent table is equivalent to the table that is the primary key, and the sub-table is equivalent to the table of the foreign key. The Dataralation constructor is generally: Dataralation (String, Datacolumn, Datacolumn), String is a relationship name, the first Datacolumn is the parent table column of the establishment of a relationship, the second Datacolumn is the two columns of the establishment of a relationship, two columns The DATATYPE value must be the same. It has been established, and this relationship must be added to the DataTable ParentRelations property or the childrelations property, which contains all the relationships of this table and the relationship of the follower table. If this table is a parent table, this relationship is added to the Childrelations collection, otherwise join the ParentRelations collection.

Second, DataView DataView represents custom views for DataTable for sorting, filtering, searching, editing, and navigation. DataView can be used with the database of the database, but the database's view can create views across tables, and DataView can only create views for a DataTable. DataView generally creates a subset of this DataTable by using the DataTable.defaultView property. The RowFilter property is used to filter the expressions to see which rows in DataTable, which is the same as the expression of the columns above. For example: "LastName = 'Smith'", this is the data line that only views the value of the column LastName is 'Smith'. The ROWSTATEFILTER property is used to set the row status filter in the DataView. When you introduce DataRow, you will have five states, a DATAROW may have five states, and RowStateFilter is to filter the rows you want to view through these states. In fact, DATAROW is not only five states, but also has version problems, such as the status of DATAROW is modified, that is, this line has been modified. At this time, this DataRow will have two versions, Current version and Original version (before modifying of). In fact, the ROWSTATEFILTER attribute is to filter status and version of DataRow (RowStateFilter's value is currentrow) See below: Member Name

Description

转载请注明原文地址:https://www.9cbs.com/read-91207.html

New Post(0)