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.