Flexible use of DataView in Visual C #

Source: Internet
Author: User
Visual

Maybe everyone knows the concept of DataView, but perhaps not many people can say clearly how well it applies in the. NET architecture. For example: How do the DataGrid and repeater these controls correlate with data? A lot of people would tell me it was through a dataset. This is clearly true, but what is the most direct link through what?

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 ();

The dataset renders the data on the control through a data view. How can we make the most of it? What if we use DataView flexibly to make our programs simpler and better? Here is an example of a program that wants to reorder a DataTable that is already in a dataset.

  


One of the written wording is:

  

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 formulation is:

  

DataView dv = ds. Tables[0]. DefaultView;

Dv. Sort = "Timeclass_name, Datetype, Tcorder";

Datagridtc.datasource = DV;

Datagridtc.databind ();

Obviously method two is much simpler in code, and more importantly, it does not create datasets and so on, reducing memory and CPU consumption.

Therefore, when you encounter the need to sort or filter the data, think more about whether to use DataView to achieve.



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.