The role of DefaultView (to sort data from a dataset query)

Source: Internet
Author: User

The role of DefaultView collection
Always in the sort of data, conditional query is directly repeated the construction of SQL to do, in the case of the number of queries and the amount of data do not feel anything, but slowly, when the program needs to be a large number of data under different conditions to check Chan or sequencing, the use of this method obviously on the performance of the program will be very obvious, Find out on the net, found that DataView can solve this problem very well, it provides a simple and intuitive way to manipulate the data, such as the following code:

SqlConnection conn = new SqlConnection ("server=.; database=mydata;uid=sa;pwd=123456; ");
Conn. Open ();
SqlDataAdapter ADP = new SqlDataAdapter ();
Adp. SelectCommand = new SqlCommand ("SELECT * FROM goods", conn);
DataSet ds = new DataSet ();
Adp. Fill (ds, "goods"); Populate DataSet Memory Scrambling library

Response.Write (ds. Tables[0]. Defaultview.count); Increase the total number of rows for the current query result
Ds. Tables[0]. Defaultview.sort = "Goodsid Desc"; This sets the sort field and the way
Ds. Tables[0]. Defaultview.rowfilter = "Shopid = 1"; Set the filter condition to get the condition Huang sufficient for "Shopid =1 all Data"

This. Gridview1.datasource = ds. Tables[0]; Binding to the GridView data source
This. Gridview1.databind ();

If we need to change the results of the query, for example, to get all the GOODSID fields greater than 10, then just change the code in the above

Ds. Tables[0]. Defaultview.rowfilter = "Shopid = 1";
Switch
Ds. Tables[0]. Defaultview.rowfilter = "Goodsid > 10";

Not require more interaction with the database server to speed up the execution of the program, and the code is simply

It may be known that the concept of DataView, but perhaps not many people can say clearly in the. NET architecture of its application scope and extent. For example: How are the DataGrid and repeater these controls associated with data? Many people will tell me that it is through a dataset. This is clearly true, but what is the most fundamental and direct link?

The answer is DataView. In fact, the following statement:

Datagridtc.datasource = Dtrst;
Datagridtc.databind ();
At work, it is equivalent to:

Datagridtc.datasource = ds. Tables[0]. DefaultView;
Datagridtc.databind ();

A dataset renders data on top of a control through a data view. So how do we make the most of it? What if the flexibility to use DataView makes our programs more concise and performance better? This is illustrated by an example of a program that wants to reorder a DataTable already in the dataset.

One of the following is a notation:

DT = ds. Tables[0]. Copy ();
Dt. Clear ();

int intnewid = 0;
for (int intI = 0;inti < ds. Tables[0]. rows.count;inti++)
{
Dr = dt. NewRow ();
dr["Datetype"] = ds. Tables[0]. Rows[inti-1 + 1]["Datetype"]. ToString ();
dr["Tcorder"] = ds. Tables[0]. Rows[inti-1 + 1]["Tcorder"]. ToString ();
dr["timeclass_id"] = Intnewid;
dr["timeclass_name"] = ds. Tables[0]. Rows[inti-1 + 1]["Timeclass_name"]. ToString ();
dr["chn_namelocal"] = ds. Tables[0]. Rows[inti-1 + 1]["chn_namelocal"]. ToString ();
dr["user_name"] = ds. Tables[0]. Rows[inti-1 + 1]["user_name"]. ToString ();
dr["user_id"] = ds. Tables[0]. Rows[inti-1 + 1]["user_id"]. ToString ();
Dt. Rows.Add (DR);
Dt. AcceptChanges ();
intnewid++;
}

Dtrst = dt. Copy ();
Dtrst.clear ();
FoundRow = dt. Select ("1 = 1", "Timeclass_name, Datetype, Tcorder");
for (int intI = 0;inti < foundrow.length;inti++)
{
Dr = Dtrst.newrow ();
dr["Datetype"] = foundrow[inti]["Datetype"]. ToString ();
dr["Tcorder"] = foundrow[inti]["Tcorder"]. ToString ();
dr["timeclass_id"] = foundrow[inti]["timeclass_id"]. ToString ();
dr["timeclass_name"] = foundrow[inti]["Timeclass_name"]. ToString ();
dr["chn_namelocal"] = foundrow[inti]["chn_namelocal"]. ToString ();
dr["user_name"] = foundrow[inti]["user_name"]. ToString ();
dr["user_id"] = foundrow[inti]["user_id"]. ToString ();
DTRST.ROWS.ADD (DR);
Dtrst.acceptchanges ();
}

Datagridtc.datasource = Dtrst;
Datagridtc.databind ();

Another notation is:

DataView dv = ds. Tables[0]. DefaultView;
Dv. Sort = "Timeclass_name, Datetype, Tcorder";
Datagridtc.datasource = DV;
Datagridtc.databind ();
Obviously the method two is more concise from the code, more importantly, it does not create a new dataset, etc., reducing the memory and CPU consumption.

So when you meet the need to sort or filter the data, think more about whether you can use DataView to achieve it.

When it comes to binding controls, it's easy to think of a dataset, but why DataView? Using DataView to operate an offline database is more convenient. The following article also explains how you can use DataView to update back to a dataset:
Role:

When working with data views, you can access these records by getting filtered or sorted records from the data View instead of directly from the table in which they reside. You can also update, insert, and delete records from a data view, subject to certain restrictions:

The data view must contain enough information about each record to be able to determine the location of the record in the data table. This information can contain a primary key or other column that together provides enough information to uniquely identify a record, such as a customer name, address, and city.
For each operation, the AllowEdit, AllowNew, and AllowDelete properties of the Data view must be set to true accordingly.
Find Records
Find records in a data view
Set the Data View's Sort property to one or more columns that you want to search.
Call the Data view's Find or FindRows method, passing the value it wants to find in the sorted column.
If you want to find a single record, call the Find method.
Or
If you want to find multiple records, use the FindRows method.
Dataview1.sort = "CustomerID";
int foundindex = Dataview1.find (TextBox1.Text);
Note: Using the Find method should first sort


Reading Records
Reading records in a data view
Use index values to point to the records you want to access in the Data view.
You can access the column by referencing columns by name in the Data view, as shown in the following example, which gets the customer name of the first record in the view:


DataView dataView1 = new DataView (ds. Customers);
string cname = dataview1[0]["CustomerName"]. ToString ();
Update record
Updating records from a data view
In your code, use the index value to identify the record that you want to update, and then set the column value by referencing the column name.
Note If the AllowEdit property of the Data view is set to False, the record cannot be edited from the Data View.
The following example shows how to identify and update a column.


dataview1[0]["CompanyName"] = "Fabrikam, Inc.";


Inserting records
Inserting records from a data view
Invokes the AddNew method of the Data view, which creates a new record and returns a DataRowView object:


DataRowView DRV;
DRV = Dataview1.addnew ();
Note: Unlike the operation on a dataset
Update records as you would any data view record.
Note If the AllowNew property of the Data view is set to False, the record cannot be inserted through the Data view.
The following example shows how to add a new record to a view and update the three fields:

C#
DataRowView DRV;
DRV = Dataview1.addnew ();
drv["CustomerID"] = "AAA";
drv["CompanyName"] = "Aafabrikam, Inc.";
drv["City" = "Aurora";

Deleting records
Delete records from a data view
Invokes the Delete method of the data view, passing it the index of the record to delete:
Note If the AllowDelete property of the Data view is set to False, the record cannot be deleted from the Data view.
The following example shows how to delete a record:

C#
Dataview1.delete (0);

The role of DefaultView (to sort data from a dataset query)

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.