GridView.RowDataBound Zdarzenie

Definicja

Występuje, gdy wiersz danych jest powiązany z danymi w kontrolce GridView .

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 

Typ zdarzenia

Przykłady

Projekt witryny sieci Web programu Visual Studio z kodem źródłowym jest dostępny do dołączenia do tego tematu: Pobierz.

W poniższym przykładzie pokazano, jak za pomocą RowDataBound zdarzenia zmodyfikować wartość pola w źródle danych przed wyświetleniem go w kontrolce GridView .


<%@ 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>

Uwagi

Przed renderowaniem kontrolki GridView każdy wiersz w kontrolce musi być powiązany z rekordem w źródle danych. Zdarzenie RowDataBound jest wywoływane, gdy wiersz danych (reprezentowany przez GridViewRow obiekt) jest powiązany z danymi w kontrolce GridView . Dzięki temu można podać metodę obsługi zdarzeń, która wykonuje niestandardową procedurę, taką jak modyfikowanie wartości danych powiązanych z wierszem, za każdym razem, gdy wystąpi to zdarzenie.

GridViewRowEventArgs Obiekt jest przekazywany do metody obsługi zdarzeń, która umożliwia dostęp do właściwości powiązanego wiersza. Aby uzyskać dostęp do określonej komórki w wierszu, użyj Cells właściwości GridViewRow obiektu zawartego GridViewRowEventArgs we Row właściwości obiektu. Można określić, który typ wiersza (wiersz nagłówka, wiersz danych itd.) jest powiązany przy użyciu RowType właściwości .

Aby uzyskać więcej informacji na temat obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Zobacz też