GridView.RowDataBound 事件

定義

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

事件類型

範例

具有原始程式碼的 Visual Studio 網站專案隨附于本主題: 下載

下列範例示範如何在控制項中顯示欄位之前,先使用 RowDataBound 事件來修改資料來源中的 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>

備註

GridView在控制項可以轉譯之前,控制項中的每個資料列都必須系結至資料來源中的記錄。 RowDataBound當) 物件所表示 GridViewRow 的資料列 (系結至 控制項中的資料 GridView 時,就會引發 事件。 這可讓您提供執行自訂常式的事件處理方法,例如在發生此事件時修改系結至資料列的資料值。

GridViewRowEventArgs物件會傳遞至事件處理方法,可讓您存取所系結之資料列的屬性。 若要存取資料列中的特定儲存格,請使用 Cells 物件之 屬性 GridViewRow 中包含的 Row 物件屬性 GridViewRowEventArgs 。 您可以使用 屬性來判斷 (標頭資料列、資料列等) RowType 的資料列類型。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱