GridView bound data Source verbose usage

Source: Internet
Author: User
Tags eval

This creates a new empty Dateset object and invokes the DataAdapter Fill method to populate the data, and then by setting the GridView data source, the DataBind method is invoked to implement the data binding.
Program code
The code is 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 tutorial 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
The code is as follows:
protected void Gridview1_pageindexchanging (object sender, Gridviewpageeventargs e)
{
Gridview1.pageindex = E.newpageindex;
}

1, use an expression. such as the next, out of stock products with red, other green.

In fact, on this expression:

(Int32) Eval ("Num") < (Int32) eval ("Minnum")? System.Drawing.Color.Red:System.Drawing.Color.Green

2, attribute members. This is the product order record, with the product number ProductID, the extended attribute product, and the unit in the Product class.

Pay attention to product.unitname there.

Instance

Open gridviewtemplatefield.asp Tutorial x and drag a GridView from the toolbox to the designer. Select and add a new ObjectDataSource control that invokes the GetEmployees () method of the Employeesbll class from the GridView smart Tag (smart tag).

498)this.width=498;" border="0">

GridView bound data: Adding a new ObjectDataSource control that calls the GetEmployees () method

The GridView binding data in this manner will automatically add a Boundfield:employeeid, LastName, FirstName, Title, HireDate, ReportsTo and country. In this report, we don't want to see the EmployeeID, ReportsTo, and country properties. To remove these BoundField, you can:

• Use the Field dialog box-click Edit Columns in the pop-up menu of the GridView smart tag. Then, in the lower left-hand corner of the list, select the BoundField you want to delete and click the button with the Red Fork to remove the BoundField.

• Manually edit the declaration statement for the GridView-in Source view, find the BoundField you want to delete, that is, the < asp:boundfield> element, delete it.

After you have deleted the BoundField of EmployeeID, ReportsTo and country, the markup language code for your gridview should look like this:

 
 
  1. < Asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" datakeynames= "EmployeeID"  
  2. datasourceid= "ObjectDataSource1" >
  3. < columns>
  4. < Asp:boundfield datafield= "LastName" headertext= "LastName" sortexpression= "LastName" />
  5. < Asp:boundfield datafield= "FirstName" headertext= "FirstName" sortexpression= "FirstName" />
  6. < Asp:boundfield datafield= "Title" headertext= "Title" sortexpression= "Title" />
  7. < Asp:boundfield datafield= "HireDate" headertext= "HireDate" sortexpression= "HireDate" />
  8. </columns>
  9. </asp:gridview>

Let's take some time to look at our results in the browser. At this point, you will see a table in which each record is an employee's information, a total of four columns: one is the employee's surname, one is the name, one is the title, and one is the date of their employment.

GridView bound data: Each employee information displays LastName, FirstName, title, and HireDate

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.