Drag a GridView control up, and then write code in Page_Load, of course, I was bound by the hand code, where the DataAdapter is used, and its constructor accepts an SQL string and connection object. With it you do not have to open and close the Connection object, DataAdapter will handle it itself, and also use the Dateset, where a new empty Dateset object is created, and the DataAdapter Fill method is called to populate the data. Then, by setting the GridView data source, the DataBind method is invoked to implement the data binding.
Program code
Copy Code code as follows:
OleDbConnection conn = new OleDbConnection ();
Conn. connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath ("Db.mdb");
OleDbDataAdapter da=new OleDbDataAdapter ("Select Art_title,art_author from [Article]", conn);
DataSet ds=new DataSet ();
Da. Fill (DS);
Gridview1.datasource=ds;
Gridview1.databind ();
Build the site, you can see the effect!
But we see the title of the table is the name of the field, so how to set the title of the table?
Cut to Design view, select the control, and then add a new column, the field type select BoundField, the header is the title of the table, the data field is the database field to bind, and after the addition completes, set the GridView AutoGenerateColumns property to False. This is to avoid the GridView automatically generate columns, and the same data is displayed at the same time, to customize the display of the field must be set!
Rebuild the site, browse, OK, no problem!
Dissatisfied with this, and then further, add pagination. Wrote the ASP's friends know that the proficient is all the way Ctrl + CTRL + V, if it is a novice, it has to write slowly!
The GridView of the. NET is less complex, and what needs to be done is simply a setup and a code.
In the PagerSettings column of the GridView, set the AllowPaging property of the GridView to True, and then set the number of records to display pagesize properties, display position position properties, and if mode is set, Define the text or picture of the paging link in turn. This setting is done, and the last step is to add the Pageindexchanging event of the GridView, which is what the GridView is going to do when clicking the page link, look at the code, just one sentence:
Program code
Copy Code code as follows:
protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gridview1.pageindex = E.newpageindex;
}
Re-build the site, you can see that the form has been added to the page!