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 개체는 바인딩되는 행의 속성에 액세스할 수 있는 이벤트 처리 메서드에 전달됩니다. 행의 특정 셀에 액세스하려면 개체의 속성에 GridViewRowRow 포함된 개체의 GridViewRowEventArgs 속성을 사용합니다Cells. 속성을 사용하여 바인딩되는 행 유형(머리글 행, 데이터 행 등)을 RowType 확인할 수 있습니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보