Compartilhar via


Criando uma Coluna Personalizada em um Controle GridView do Servidor Web

The GridView control can automatically generate columns from the fields provided by the data source.In addition, you can create a collection of columns to be displayed instead of automatically generating them.However, you might encounter a scenario where you need to customize how an individual column is displayed.In that case, you can create a TemplateField to specify a custom column layout.

Criando modelos

A TemplateField object enables you to specify templates that contain markup and controls to customize the layout and behavior of a column in a GridView control.Using an ItemTemplate, you can specify the layout to be used when the GridView displays data in a column.To specify custom layout for when users edit data in a column, you can create an EditItemTemplate.

O modelo pode conter marcações, controles de servidor da Web, e botões de comando.Para obter mais informações sobre modelos, consulte Modelos de controles servidores web ASP.NET.

Data-Binding in a Template

In a template, you can bind controls to data using the Eval and Bind methods.You use the Eval method when the control will only display values.You use the Bind method when users can modify a data value—that is, for data-update scenarios.You can use the Eval method in any of the templates to display data.You use the Bind method in a template with controls in which users might change values, such as TextBox and CheckBox controls, or a template that allows a record to be deleted.Para obter mais informações, consulte Visão geral de expressões de ligação de dados.

Exemplo

The following example shows the Columns collection of a GridView control.The collection contains a TemplateField object, which in turn contains an ItemTemplate object.To display a date, the ItemTemplate includes a Label control that uses the Eval method.To edit a date, the other templates use a Calendar control that uses the Bind method.

<Columns>                  
  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
  <asp:TemplateField HeaderText="Birth Date">
    <ItemTemplate> 
      <asp:Label ID="BirthDateLabel" Runat="Server" 
                 Text='<%# Eval("BirthDate", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate>
      <asp:Calendar ID="EditBirthDateCalendar" Runat="Server"
                    VisibleDate='<%# Eval("BirthDate") %>'
                    SelectedDate='<%# Bind("BirthDate") %>' />
    </EditItemTemplate>
  </asp:TemplateField>   
  <asp:HyperLinkField Text="Show Detail"
                      DatahrefFormatString="~/ShowEmployeeDetail.aspx?EmployeeID={0}"
                      DatahrefFields="EmployeeID" />                   
</Columns> 
<Columns>                  
  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
  <asp:TemplateField HeaderText="Birth Date">
    <ItemTemplate> 
      <asp:Label ID="BirthDateLabel" Runat="Server" 
                 Text='<%# Eval("BirthDate", "{0:d}") %>' />
    </ItemTemplate>
    <EditItemTemplate>
      <asp:Calendar ID="EditBirthDateCalendar" Runat="Server"
                    VisibleDate='<%# Eval("BirthDate") %>'
                    SelectedDate='<%# Bind("BirthDate") %>' />
    </EditItemTemplate>
  </asp:TemplateField> 
  <asp:HyperLinkField Text="Show Detail"
                      DatahrefFormatString="~/ShowEmployeeDetail.aspx?EmployeeID={0}"
                      DatahrefFields="EmployeeID" />                   
</Columns> 

Consulte também

Conceitos

Visão geral de controle servidor Web com dados vinculados

Visão geral sobre controles fonte de dados

Referência

Visão Geral sobre o Controle do Servidor Web GridView