In actual programming projects, we often encounter the following situation: datatable is not in the database, or datatable has not been written to the database, or the able read from the database has been changed locally, I have not written it back to the database (other changes may be required). In these cases, I only need to use it. the powerful functions of the dataview class provided by the net class library (mainly using its rowfilter attribute) can easily solve such query problems.
Some netizens asked online how to execute able. Select ("condition") in datatable to return the datatable. Today I am here to provide a solution for your reference:
The Code is as follows:
Code
//// <Summary>
/// Execute the query in the datatable to return the new datatable
/// </Summary>
/// <Param name = "DT"> source data datatable </param>
/// <Param name = "condition"> query condition </param>
/// <Returns> </returns>
Private datatable getnewdatatable (datatable DT, string condition)
{
Datatable newdt = new datatable ();
Newdt = DT. Clone ();
Datarow [] DR = DT. Select (condition );
For (INT I = 0; I <dr. length; I ++)
{
Newdt. importrow (datarow) Dr [I]);
}
Return newdt; // The returned query result.
}
Hey, they are all the same. Add rows cyclically.
Datarow [] rows = DT. Select (conditions );
Foreach (datarow row in rows)
{
Newdt. Rows. Add (row. itemarray );
}
Return newdt;
-----------------------------------------------
Davin: This afternoon, I also encountered this problem. Regarding the datatable filtering problem, I asked to retrieve the first 20 results after filtering, always hovering between the filterrow and select methods, filterrow has poor performance. Select () returns datarow [] is not easy to operate, but you can see your practice, DT. clone ();
The performance is really serious. If dt is a large data table
--------------------------------------------------------
DT. Clone () should have no performance problems.
The clone () method only copies structure information and does not copy data.