I. Introduction
Binding repeater to datareader is a common and efficient form of data binding. Sometimes you can use the data source control (datasource) of ASP. net2.0, but it does not feel flexible enough.
In general, repeater and datareader can be directly bound:
Sqldatareader Dr = New Sqlutility. executereader (...);
Repeater. datasource = Dr;
Repeater. databind ();
However, sometimes, when repeater data is bound for secondary classification, dropdownlist must also be bound to the same data source, and datareader must directly read the data again, if the data read by datareader is saved to the able, the data can be bound n times.
II. Implementation
1. Fill the datatable with datareader directly
CodeAs follows:
// Piao: http://www.cnblogs.com/zxjay Public Datatable datareadertodatatable (idatareader reader)
{
Datatable TB = New Datatable ();
Datacolumn Col;
Datarow row;
Int I;
For (I = 0 ; I < Reader. fieldcount; I ++ )
{
Col = New Datacolumn ();
Col. columnname = Reader. getname (I );
Col. datatype = Reader. getfieldtype (I );
TB. Columns. Add (COL );
}
While (Reader. Read ())
{
Row = TB. newrow ();
For (I = 0 ; I < Reader. fieldcount; I ++ )
{
Row [I] = Reader [I];
}
TB. Rows. Add (ROW );
}
Return TB;
}
2. asp. net2.0 has a simpler method:
Datatable dt = New Datatable ();
DT. Load (idatareader );
One statement.
For more information about datatable. Load (...), see the open-source Mono Project (http://www.go-mono.com /).
File Location: (mono-1.2.4 \ MCS \ class \ system. Data \ system. Data \ datatable. CS)
3. Summary:
No pai_^