GridView.RowDataBound (Evento)
Se produce cuando una fila de datos se enlaza a los datos de un control GridView.

Espacio de nombres: System.Web.UI.WebControls
Ensamblado: System.Web (en system.web.dll)

Sintaxis

Visual Basic (Declaración)
Public Event RowDataBound As GridViewRowEventHandler
Visual Basic (Uso)
Dim instance As GridView
Dim handler As GridViewRowEventHandler

AddHandler instance.RowDataBound, handler
C#
public event GridViewRowEventHandler RowDataBound
C++
public:
event GridViewRowEventHandler^ RowDataBound {
    void add (GridViewRowEventHandler^ value);
    void remove (GridViewRowEventHandler^ value);
}
J#
/** @event */
public void add_RowDataBound (GridViewRowEventHandler value)

/** @event */
public void remove_RowDataBound (GridViewRowEventHandler value)
JScript
En JScript, se pueden controlar los eventos que define una clase, pero no se pueden definir unos propios.
XAML
No aplicable.
Comentarios

Para poder representar el control GridView, cada fila del control debe enlazarse a un registro del origen de datos. El evento RowDataBound se produce cuando una fila de datos (representada por un objeto GridViewRow) se enlaza a los datos del control GridView. Esto permite proporcionar un método de control de eventos que realice una rutina personalizada, como modificar los valores de los datos enlazados a la fila, cada vez que se produzca este evento.

Se pasa un objeto GridViewRowEventArgs al método de control de eventos que permite tener acceso a las propiedades de la fila que se va a enlazar. Para tener acceso a una celda específica de la fila, utilice la propiedad Cells del objeto GridViewRow contenido en la propiedad Row del objeto GridViewRowEventArgs. Puede determinar qué tipo de fila (fila de encabezado, fila de datos, etc.) se va a enlazar mediante la propiedad RowType.

Para obtener más información sobre la forma de controlar eventos, vea Utilizar eventos.

Ejemplo

En el ejemplo de código siguiente se muestra cómo utilizar el evento RowDataBound para modificar el valor de un campo en el origen de datos antes de mostrarse en un control GridView.

Visual Basic
<%@ 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  >
  <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>
C#
<%@ 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  >
  <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>
Plataformas

Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

Información de versión

.NET Framework

Compatible con: 3.0, 2.0
Vea también

Page view tracker