=== Premise: we already have a datatable data table. All we need now is a row of Data ===
For example, the following sectionCodeIn a method, a datatable is returned!
Sqlcon = New Sqlconnection (strcon );
Sqlcom = New Sqlcommand ();
Sqlcom. Connection = Sqlcon;
Sqlcom. commandtext = " Select_v " ;
Sqlcom. commandtype = Commandtype. storedprocedure;
Sqlcon. open ();
SDR = Sqlcom. executereader ();
DT = New Datatable ( );
DT. Load (SDR );
Assume that the returned datatable is DT,
Method 1: Copy A able, clear the data (the column structure is retained), and add the required row from DT using the importrow () method;
Datatable dt2 = new datatable ();
Dt2 = DT. Copy ();
Dt2.rows. Clear ();
Dt2.importrow (Dt. Rows [0]); // This is the first line added
Method 2:
First, the new able must be added to the corresponding column name (column)
An empty and non-structured able cannot directly add a row to it!
For ( Int I = 0 ; I < DT. Columns. Count; I ++ )
{
Dt2.columns. Add (Dt. Columns [I]. columnname); // you can add the column data type to the overload method.
}
Then you can join the row directly!
// 1
Datarow drq = Dt2.newrow ();
Drq. itemarray = DT. Rows [ 0 ]. Itemarray; // This is the first line added
Dt2.rows. Add (drq );
// 2
Dt2.importrow (Dt. Rows [ 0 ]); // This is the first line added
Finally, you can put the new able into Dataset:
DS = new dataset ();
DS. Tables. Add (dt2 );
A simple method to filter repeated rows of a rows table
Dataview View=Table. defaultview;
Datatable tagettable=View. totable (True,"Column1","Column2",...);
Source Program Resources from the network are recorded for future use. May be useful to everyone // Table datatablesource to obtain data String connectionstring = "provider = Microsoft. Jet. oledb.4.0; persist Security info = false;" + "Data Source = D: \ vb60 \ MDB \ northwind. mdb "; Oledbconnection conn = new oledbconnection (connectionstring ); Conn. open (); Oledbdataadapter Ada = new oledbdataadapter ("select * from orders", Conn ); Datatable datatablesource = new datatable (); Ada. Fill (datatablesource ); Datagrid1.datasource = datatablesource; // Create a new table datatabledest Datatable datatabledest = new datatable (); // Copy the structure of the datatablesource table to the datatabledest table. Datatabledest = datatablesource. Clone (); // Copy the data to the new table. Foreach (datarow DR in datatablesource. Rows) { // Use importrow () to copy data. If ableabledest. Rows. Add (DR) is used, an error occurs: system. argumentexception: This row already belongs to another table. Datatabledest. importrow (DR ); } Datagrid2.datasource = datatabledest; Conn. Close (); |