The eighth day of the 10 day society asp.net

Source: Internet
Author: User
asp.net learning objective: To master the use of the DataGrid

In the previous ASP, we showed that mass data often used a cyclic reading of the recordset, inserting code into the table to show that the loop was <tr&gt, and if it was paginated, it would have to be made, if the order was more complex, In ASP.net, all work can be done by the DataGrid.

First look at the style properties of the DataGrid
Backimageurl= "" Background picture
cellspacing= "" Cell spacing
cellpadding= "" Cell padding
CSS style used by cssclass= ""

The DataGrid can automatically put the field names in the table in the header of the displayed record to indicate the meaning of each cell, using showheader= "True/false" to control whether it is displayed, in most cases we do not need this function, Because the field names in our database are mostly in English, and we want to output most of the page is the Chinese name.

Here's a look at all the records in the database, just a few lines of code:


<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>
<body>
<asp:datagrid
Id= "Dgrdmain"
cellpadding= "1"
Showheader= "true"
Borderwidth= "0"
runat= "Server"
/>
</body>

As for the VB version, this time everyone to try it yourself:

Suppose the database is three fields: ID,AA,BB
The display looks as follows:


ID AA bb
1 Werwe Rewrwe
2 Werwe Rewrwe



We must feel that this display is not satisfactory, we have two ways of showing (need to include in <columns></columns> Center):

One, the default column, we can choose not to output all the fields can also be arranged in order:
<asp:boundcolumn datafield= "field name to display" >
For example, we want to output this table according to BB,AA, so we write

<asp:datagrid
Id= "Dgrdmain"
cellpadding= "1"
Showheader= "true"
Autogeneratecolumns= "false"
Borderwidth= "0"
runat= "Server" >
<columns>
<asp:boundcolumn datafield= "BB"/>
<asp:boundcolumn datafield= "BB"/>
</columns>
</asp:datagrid>

Note the DataGrid will not automatically output all fields after using the autogeneratecolumns= "false".

Second, with the template column, we can customize the style of each cell:
<asp:TemplateColumn>
<itemTemplate>
There's a table in the middle, so do what you want.
</itemTemplate>
</asp:DataGrid>
Maybe you want to say how to output the code in the table, you can use <%# DataBinder.Eval (Container.DataItem, field name). ToString ()%>
Here's an example, all we have to do is put the aa,bb two fields in a cell, like the following:


1 Werwe
Rewrwe
2 Werwe
Rewrwe


We write code like this:
<asp:datagrid
Id= "Dgrdmain"
cellpadding= "1"
Showheader= "false"
Autogeneratecolumns= "false"
Borderwidth= "0"
runat= "Server" >
<columns>
<asp:boundcolumn datafield= "II"/> first displays the ID using the default display method
<asp:TemplateColumn> use a template to display the following column (composed of AA,BB)
<itemTemplate>
<table border= "0" cellspacing= "0" cellpadding= "0" width= "100%" >
<tr>
<td><%# Container.DataItem ("AA")%></td>
</tr>
<tr>
<td><%# container.dataitem ("BB")%></td>
</tr>
</table>
</itemTemplate>
</asp:DataGrid>
</columns>
</asp:datagrid>

Let's talk about this today, and tomorrow we'll look at the paging display features of the DataGrid


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.