Implementation of Data Binding in GridView

Source: Internet
Author: User

When you need to use some TemplateField to customize the display, I found that the simplest thing is to first create a GridView control that only contains BoundField, and then add some TemplateField, if necessary, some boundfields can also be directly converted to TemplateField. Now, let's start this tutorial. First, add a GridView control to the page through the designer and bind an ObjectDataSource that returns employee information to it. These steps create a GridView with some boundfields, which correspond to different fields in the employee information.

Open GridViewTemplateField. aspx and drag a GridView from the toolbox to the designer. Select and add a new ObjectDataSource control that calls the GetEmployees () method of the EmployeesBLL class from the smart tag of the GridView.

 

Data Binding in the GridView: Add a new ObjectDataSource control that calls the GetEmployees () method.

In this way, the data bound to the GridView will automatically add a BoundField for each attribute of the employee information: EmployeeID, LastName, FirstName, Title, HireDate, ReportsTo, and Country. In this report, we do not want to see the attributes of EmployeeID, ReportsTo, and Country. To delete these boundfields, you can:

· Use field dialog box-click "Edit Columns" in the displayed menu of the intelligent Tab Of The GridView ). Then, select the BoundField you want to delete in the list in the lower left corner and click the Red Cross button to delete the BoundField.

· Manually edit the declaration statement of the GridView-in the Source view), find the BoundField you want to delete, which is the <asp: BoundField> element, and delete it.

After you delete BoundField such as EmployeeID, ReportsTo, and Country, the markup language code of 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 a moment to look at our results in the browser. At this time, you will see a table where every record in the table is an employee's information. There are four columns: one is the employee's surname, the other is the name, and the other is the title, another is their employment date.

 

Data bound to the GridView: each employee information displays LastName, FirstName, Title, and HireDate

  1. Use ASP. NET DetailsView to display details
  2. Familiar with the use of DropDownList and ObjectDataSource
  3. ASP. NET DropDownList creation and filling
  4. Use the GridView and ObjectDataSource in ASP. NET
  5. Example of ASP. NET DropDownList

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.