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

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

Un progetto di sito Web di Visual Studio con il codice sorgente è disponibile a complemento di questo argomento: Download.A Visual Studio Web site project with source code is available to accompany this topic: Download.

L'esempio seguente illustra come usare il RowDataBound eventi per modificare il valore di un campo nell'origine dati prima che venga visualizzato 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 controllo possa essere sottoposti a rendering, ogni riga nel controllo deve essere associata 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. Il RowDataBound evento viene generato quando una riga di dati (rappresentato da un GridViewRow oggetto) è associato a dati nel GridView controllo.The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. In questo modo è possibile fornire un metodo di gestione degli eventi che esegue una routine personalizzata, ad esempio la modifica dei valori dei dati 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.

Oggetto GridViewRowEventArgs oggetto viene passato al metodo gestione degli eventi, che consente di accedere alle proprietà della riga a cui è 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 nella riga specifica, usare il Cells proprietà del GridViewRow contenuta nell'oggetto la Row proprietà del GridViewRowEventArgs 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) viene associato tramite 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