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.