Previously, in ASP, we showed that large volumes of data were frequently read through record sets cyclically and inserted into tables.
Code To display, the loop is <tr>. If it is a page, you still need to make it by yourself. If the sorting is more complex, it will be fine in ASP. NET. Everything can be done
DataGrid .
First, let's take a look at the style attributes of the DataGrid.
Backimageurl = "" background image
Cellspacing = "" cell spacing
Cellpadding = "" cell Filling
Cssclass = "" CSS style
The DataGrid can automatically display the meaning of each cell by placing the field names in the table in the header of the displayed record. Showheader = "True/false" In most cases, we do not need this function because most of the field names in our database are in English, and most of the output on the page is in Chinese. The following code shows all records in the database:
<SCRIPT runat = "server" Language = "C #">
Void page_load ()
{
String strconnection = "provider = Microsoft. Jet. oledb.4.0; Data Source = ";
Strconnection + = server. mappath ("guestbook. mdb ");
Oledbconnection objconnection = new oledbconnection (strconnection );
Oledbcommand objcommand1 = new oledbcommand ("select * From Guestbook", objconnection );
Objconnection. open ();
Dgrdmain. datasource = objcommand1.executereader (); // dgrdmain is the ID of the DataGrid below
Dgrdmain. databind ();
Objconnection. Close ();
}
</SCRIPT>
<HTML>
<Body>
<Asp: DataGrid
Id = "dgrdmain"
Cellpadding = "1"
Showheader = "true"
Borderwidth = "0"
Runat = "server"
/>
</Body>
</Html>
For the vb version, please try it by yourself this time :)
Assume that the database has three fields: ID, AA, and BB.
The display is as follows:
Idaabb
1 werwerewrwe
2 werwerewrwe
we must feel that this display is not satisfactory. We have two display methods (which must be included in the middle of ):
1. Default columns. We can choose not to output all fields or arrange the order:
for example, if we want to output this table according to BB and AA, we should write this statement
id = "dgrdmain"
cellpadding = "1"
showheader = "true"
autogeneratecolumns = "false"
borderwidth = "0 "
runat =" server ">
Note: After autogeneratecolumns = "false" is used, the DataGrid will not automatically output all fields.
2. Use the template column to customize the style of each cell:
there is a table in the middle. What should I do?
If You Want To output code in the table, you can use <% # databinder. eval (container. dataitem, "field name "). tostring () %>
let's take a look at the example below. All we need to do is put the AA and BB fields in one cell for display, just like the following:
1 werwe
rewrwe
2 werwe
rewrwe
Let's write the code like this:
id = "dgrdmain"
cellpadding = "1"
showheader = "false"
autogeneratecolumns = "false"
borderwidth = "0 "
runat =" server ">
first use the default display mode to display the id
and then use the template to display the next column (consisting of AA and BB)
| <% # container. dataitem ("AA") %> |
| <% # container. dataitem ("BB") %> |