Flexible Use of dataview in Visual C #

Source: Internet
Author: User

Maybe everyone knows the concept of dataview, but maybe not many people can clearly explain its application scope and degree in the. NET architecture. For example, how are the DataGrid and repeater controls associated with data? Many people will tell me that dataset is used. This is obviously correct, but what is the most fundamental and direct connection?

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

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

At work, it is equivalent:

Datagridtc. datasource = Ds. Tables [0]. defaultview;
Datagridtc. databind ();

Dataset presents data on the control through the data view. So how can we make the best use of things? What if we use dataview flexibly to make our program simpler and better performance? The following example shows that a program wants to re-Sort an existing datatable in dataset.

A statement is as follows:

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 statement is as follows:

Dataview DV = Ds. Tables [0]. defaultview;
DV. Sort = "timeclass_name, datetype, tcorder ";
Datagridtc. datasource = DV;
Datagridtc. databind ();

Obviously, method 2 is much more concise in code. More importantly, it does not need to create new dataset, which reduces memory and CPU consumption.

Therefore, when you need to sort or filter data, consider whether dataview can be used.

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.