GridView.RowDataBound Evento

Definizione

Si verifica quando una riga di dati viene associata a dati in un controllo 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 

Esempi

Per accompagnare questo argomento, è disponibile un progetto di sito Web di Visual Studio con codice sorgente: Scaricare.A Visual Studio Web site project with source code is available to accompany this topic: Download.

Nell'esempio seguente viene illustrato come utilizzare l' RowDataBound evento per modificare il valore di un campo nell'origine dati prima che venga visualizzato in un GridView controllo.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>

Commenti

Prima di GridView poter eseguire il rendering del controllo, è necessario associare ogni riga del controllo a un record nell'origine dati.Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. L' RowDataBound evento viene generato quando una riga di dati (rappresentata GridViewRow da un oggetto) viene GridView associata ai dati nel controllo.The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. Questo consente di fornire un metodo di gestione degli eventi che esegue una routine personalizzata, ad esempio la modifica dei valori associati alla riga, ogni volta che si verifica questo 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 oggetto viene passato al metodo di gestione degli eventi, che consente di accedere alle proprietà della riga associata.A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. Per accedere a una cella specifica nella riga, utilizzare la Cells proprietà GridViewRow dell'oggetto contenuto GridViewRowEventArgs nella Row proprietà dell'oggetto.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. È possibile determinare il tipo di riga (riga di intestazione, riga di dati e così via) da associare utilizzando la RowType proprietà.You can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.For more information about how to handle events, see Handling and Raising Events.

Si applica a

Vedi anche