GridView.RowDataBound 事件

定義

資料列繫結至 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 

事件類型

GridViewRowEventHandler

範例

本主題隨附有原始程式碼的 Visual Studio 網站專案: 下載A Visual Studio Web site project with source code is available to accompany this topic: Download.

下列範例示範如何使用 RowDataBound 事件來修改資料來源中欄位的值,然後再將它顯示在 GridView 控制項中。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>

備註

GridView控制項中的每個資料列都必須系結至資料來源中的記錄,才能轉譯控制項。Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. RowDataBound當) 物件所代表的資料列 (系結 GridViewRow 至控制項中的資料時,就會引發事件 GridViewThe RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. 這可讓您提供事件處理方法,以執行自訂常式,例如,每當發生此事件時,修改系結至資料列的資料值。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.

GridViewRowEventArgs物件會傳遞至事件處理方法,可讓您存取所系結之資料列的屬性。A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. 若要存取資料列中的特定資料格,請使用物件屬性(property)中所 Cells 包含之物件的屬性(property) GridViewRow Row GridViewRowEventArgsTo access a specific cell in the row, use the Cells property of the GridViewRow object contained in the Row property of the GridViewRowEventArgs object. 您可以使用屬性來判斷 (標頭資料列、資料列等) 的資料列類型 RowTypeYou can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

如需如何處理事件的詳細資訊,請參閱 處理和引發事件For more information about how to handle events, see Handling and Raising Events.

適用於

另請參閱