GridView.HeaderRow プロパティ

定義

GridViewRow コントロールのヘッダー行を表す GridView オブジェクトを取得します。

public:
 virtual property System::Web::UI::WebControls::GridViewRow ^ HeaderRow { System::Web::UI::WebControls::GridViewRow ^ get(); };
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.WebControls.GridViewRow HeaderRow { get; }
[<System.ComponentModel.Browsable(false)>]
member this.HeaderRow : System.Web.UI.WebControls.GridViewRow
Public Overridable ReadOnly Property HeaderRow As GridViewRow

プロパティ値

GridViewRow コントロールのヘッダー行を表す GridView

属性

次の例では、 プロパティを HeaderRow 使用して、並べ替えの方向に基づいてヘッダー行のフォントの色をプログラムで変更する方法を示します。


<%@ 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_DataBound(Object sender, EventArgs e)
  {
    
    // Get the header row.
    GridViewRow headerRow = CustomersGridView.HeaderRow;   
    
    // Get the footer row.
    GridViewRow footerRow = CustomersGridView.FooterRow; 

    // Set the font color of the header and footer rows
    // based on the sort direction. 
    switch (CustomersGridView.SortDirection)
    {
      case SortDirection.Ascending:
        headerRow.ForeColor = System.Drawing.Color.Green;
        footerRow.ForeColor = System.Drawing.Color.Green;
        break;
      case SortDirection.Descending:
        headerRow.ForeColor = System.Drawing.Color.Red;
        footerRow.ForeColor = System.Drawing.Color.Red;
        break;
      default:
        headerRow.ForeColor = System.Drawing.Color.Black;
        footerRow.ForeColor = System.Drawing.Color.Black;
        break;
    }

    // Display the sort order in the footer row.
    footerRow.Cells[0].Text = "Sort Order = " + CustomersGridView.SortDirection.ToString();
      
  }
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView HeaderRow and FooterRow Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView HeaderRow and FooterRow Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowsorting="true"
        allowpaging="true" 
        showheader="true"
        showfooter="true"
        ondatabound="CustomersGridView_DataBound"    
        runat="server">
        
        <headerstyle backcolor="LightCyan"
          forecolor="MediumBlue"/>
                    
        <footerstyle backcolor="LightCyan"
          forecolor="MediumBlue"/>
                        
      </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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </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_DataBound(ByVal sender As Object, ByVal e As EventArgs)
      
    ' Get the header row.
    Dim headerRow As GridViewRow = CustomersGridView.HeaderRow
    
    ' Get the footer row.
    Dim footerRow As GridViewRow = CustomersGridView.FooterRow

    ' Set the font color of the header and footer rows
    ' based on the sort direction. 
    Select Case CustomersGridView.SortDirection

      Case SortDirection.Ascending
        headerRow.ForeColor = System.Drawing.Color.Green
        footerRow.ForeColor = System.Drawing.Color.Green
      Case SortDirection.Descending
        headerRow.ForeColor = System.Drawing.Color.Red
        footerRow.ForeColor = System.Drawing.Color.Red
      Case Else
        headerRow.ForeColor = System.Drawing.Color.Black
        footerRow.ForeColor = System.Drawing.Color.Black
        
    End Select

    ' Display the sort order in the footer row.
    footerRow.Cells(0).Text = "Sort Order = " & CustomersGridView.SortDirection.ToString()
      
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView HeaderRow and FooterRow Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView HeaderRow and FooterRow Example</h3>

      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowsorting="true"
        allowpaging="true" 
        showheader="true"
        showfooter="true"
        ondatabound="CustomersGridView_DataBound"    
        runat="server">
        
        <headerstyle backcolor="LightCyan"
          forecolor="MediumBlue"/>
                    
        <footerstyle backcolor="LightCyan"
          forecolor="MediumBlue"/>
                        
      </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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
        
    </form>
  </body>
</html>

注釈

コントロールのヘッダー行を HeaderRow 表す オブジェクトにプログラムでアクセス GridViewRow するには、 プロパティを GridView 使用します。

Note

プロパティは HeaderRow 、コントロールが イベントに GridView ヘッダー行 RowCreated を作成した後にのみ使用できます。

このプロパティは、カスタム コンテンツを追加する場合など、ヘッダー行をプログラムで操作する必要がある場合に一般的に使用されます。 プロパティに HeaderRow 対する変更は、コントロールが GridView レンダリングされた後に実行する必要があります。それ以外の GridView 場合、コントロールは変更を上書きします。

適用対象

こちらもご覧ください