Ten days to learn ASP.net (8)

Source: Internet
Author: User

Daily

Objective: To learn how to use DATAGRID

In the past, in ASP, we often used the method of reading Record Sets cyclically when displaying large volumes of data. We inserted code into the table to display the results. The loop is <tr>, if it is a page, you still need to make it by yourself. If the sorting is more complex, in ASP. NET. All the work can be done by the 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. It uses ShowHeader = "true/false" to control whether to display the table, 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:

Id Aa Bb
1 Werwe Rewrwe
2 Werwe Rewrwe



We must feel that this display is not satisfactory. We have two display methods (which must be included in the middle of <columns> </columns> ):

1. For the default column, we can choose not to output all fields or arrange the order:
<Asp: BoundColumn DataField = "field name to be displayed">
For example, we want to output this table according to bb and aa.

<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 that after autogeneratecolumns = "false" is used, the DATAGRID will not automatically output all fields.

2. Using the template column, we can customize the style of each cell:
<Asp: TemplateColumn>
<ItemTemplate>
There is a table in the middle, just like what you want
</ItemTemplate>
</Asp: DataGrid>
If you want to output code in the table, you can use <% # DataBinder. Eval (Container. DataItem, "field name"). ToString () %>
Let's look at an example. 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


Write the Code as follows:
<Asp: DataGrid
Id = "dgrdMain"
Cellpadding = "1"
Showheader = "false"
Autogeneratecolumns = "false"
Borderwidth = "0"
Runat = "server">
<Columns>
<Asp: boundcolumn datafield = "ii"/> first, use the default display method to display the ID.
<Asp: TemplateColumn> use the template to display the next column (consisting of AA and 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. Let's take a look at the page display and other functions of the DATAGRID tomorrow.


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.