GridViewRowEventHandler Delegato

Definizione

Rappresenta il metodo che gestisce gli eventi RowCreated e RowDataBound di un controllo GridView.

public delegate void GridViewRowEventHandler(System::Object ^ sender, GridViewRowEventArgs ^ e);
public delegate void GridViewRowEventHandler(object sender, GridViewRowEventArgs e);
type GridViewRowEventHandler = delegate of obj * GridViewRowEventArgs -> unit
Public Delegate Sub GridViewRowEventHandler(sender As Object, e As GridViewRowEventArgs)

Parametri

sender
Object

Origine dell'evento.

e
GridViewRowEventArgs

Oggetto GridViewRowEventArgs contenente i dati dell'evento.

Esempio

Nell'esempio seguente viene illustrato come aggiungere un GridViewRowEventHandler delegato a livello di codice all'evento RowDataBound di un GridView controllo .


<%@ 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 Page_Load(Object sender, EventArgs e)
  {
        
    // Create a new GridView control.
    GridView customersGridView = new GridView();
         
    // Set the GridView control's properties.
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSqlDataSource";
    customersGridView.AutoGenerateColumns = true;
    customersGridView.AllowPaging = true;

    // Programmatically register the event-handling method.
    customersGridView.RowDataBound += new GridViewRowEventHandler(this.CustomersGridView_RowDataBound);
       
    // Add the GridView control to the Controls collection
    // of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView);

  }

  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>GridViewRowEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowEventHandler Example</h3>

      <asp:placeholder id="GridViewPlaceHolder"
        runat="server"/>
            
      <!-- 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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        
    ' Create a new GridView control.
    Dim customersGridView As New GridView()
         
    ' Set the GridView control's properties.
    customersGridView.ID = "CustomersGridView"
    customersGridView.DataSourceID = "CustomersSqlDataSource"
    customersGridView.AutoGenerateColumns = True
    customersGridView.AllowPaging = True

    ' Programmatically register the event-handling method.
    AddHandler customersGridView.RowDataBound, AddressOf CustomersGridView_RowDataBound
       
    ' Add the GridView control to the Controls collection
    ' of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView)

  End Sub

  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>GridViewRowEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewRowEventHandler Example</h3>

      <asp:placeholder id="GridViewPlaceHolder"
        runat="server"/>
            
      <!-- 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>

Nell'esempio seguente viene illustrato come aggiungere in modo dichiarativo un GridViewRowEventHandler delegato all'evento RowDataBound di un GridView controllo .


<%@ 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 poter eseguire il rendering del GridView controllo, è necessario creare un GridViewRow oggetto per ogni riga nel controllo . L'evento RowCreated viene generato ogni volta che viene creata una riga nel GridView controllo . In questo modo è possibile fornire un metodo di gestione degli eventi che esegue una routine personalizzata, ad esempio l'aggiunta di contenuto personalizzato a una riga, ogni volta che si verifica questo evento.

Analogamente, ogni riga del controllo deve essere associata a un record nell'origine dati prima di poter eseguire il rendering del GridView controllo. L'evento RowDataBound viene generato quando una riga di dati (rappresentata da un GridViewRow oggetto ) è associata ai dati nel GridView controllo . In questo modo è possibile fornire un metodo di gestione degli eventi che esegue una routine personalizzata, ad esempio modificando i valori dei dati associati alla riga, ogni volta che si verifica questo evento.

Quando si crea un delegato GridViewRowEventHandler, si identifica il metodo che gestirà l'evento. Per associare l'evento al gestore eventi in uso, aggiungere all'evento un'istanza del delegato. Il gestore eventi viene chiamato ogni volta che si verifica l'evento, a meno che non venga rimosso il delegato. Per altre informazioni sui delegati del gestore eventi, vedere Gestione e generazione di eventi.

Metodi di estensione

GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.

Si applica a

Vedi anche