GridView.RowDataBound GridView.RowDataBound GridView.RowDataBound GridView.RowDataBound Event

Definición

Se produce cuando una fila de datos se enlaza a los datos de un control GridView.Occurs when a data row is bound to data in a GridView control.

public:
 event System::Web::UI::WebControls::GridViewRowEventHandler ^ RowDataBound;
public event System.Web.UI.WebControls.GridViewRowEventHandler RowDataBound;
member this.RowDataBound : System.Web.UI.WebControls.GridViewRowEventHandler 
Public Custom Event RowDataBound As GridViewRowEventHandler 

Ejemplos

Un proyecto de sitio web de Visual Studio con código fuente está disponible para acompañar este tema: Descargar.A Visual Studio Web site project with source code is available to accompany this topic: Download.

En el ejemplo siguiente se muestra cómo utilizar RowDataBound el evento para modificar el valor de un campo en el origen de datos antes de que se GridView muestre en un control.The following example demonstrates how to use the RowDataBound event to modify the value of a field in the data source before it is displayed in a GridView control.


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
        
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
        
    }
    
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowDataBound Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDataBound Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        allowpaging="true"
        onrowdatabound="CustomersGridView_RowDataBound" 
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
            
    </form>
  </body>
</html>


<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then
    
      ' Display the company name in italics.
      e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
        
    End If
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowDataBound Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDataBound Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        allowpaging="true"
        onrowdatabound="CustomersGridView_RowDataBound" 
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
            
    </form>
  </body>
</html>

Comentarios

Antes de GridView que se pueda representar el control, cada fila del control debe estar enlazada a un registro del origen de datos.Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. El RowDataBound evento se desencadena cuando una fila de datos (representada por un GridViewRow objeto) se enlaza a GridView los datos del control.The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. Esto le permite proporcionar un método de control de eventos que realiza una rutina personalizada, como modificar los valores de los datos enlazados a la fila, siempre que se produzca este evento.This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

Un GridViewRowEventArgs objeto se pasa al método de control de eventos, que permite tener acceso a las propiedades de la fila que se está enlazando.A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. Para tener acceso a una celda específica de la fila, Cells use la propiedad GridViewRow del objeto contenido GridViewRowEventArgs en Row la propiedad del objeto.To access a specific cell in the row, use the Cells property of the GridViewRow object contained in the Row property of the GridViewRowEventArgs object. Puede determinar qué tipo de fila (fila de encabezado, fila de datos, etc.) se está enlazando mediante RowType la propiedad.You can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.For more information about how to handle events, see Handling and Raising Events.

Se aplica a

Consulte también: