Export (0) Print
Expand All

GridViewRow.RowType Property

Gets the row type of the GridViewRow object.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public virtual DataControlRowType RowType { get; set; }
<asp:GridViewRow RowType="DataControlRowType" />

Use the RowType property to determine the type of row that the GridViewRow object represents. The following table lists the different row type values using the DataControlRowType enumeration.

Row type

Description

DataRow

A data row in the GridView control.

Footer

The footer row in the GridView control.

Header

The header row in the GridView control.

EmptyDataRow

The empty row in the GridView control. The empty row is displayed when GridView control does not have any records to display.

Pager

A pager row in the GridView control.

Separator

A separator row in the GridView control.

This property is commonly used to determine a row's type before performing an operation.

The following example demonstrates how to use the RowType property to determine whether a row being created is a footer row. If the row is a footer row, the value for the sum of the column is updated into the footer row.


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

  // Create a variable to store the order total.
  private Decimal orderTotal = 0.0M;

  void OrderGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    // Retrieve the current row. 
    GridViewRow row = e.Row;

    // Update the column total if the row being created is
    // a footer row.
    if (row.RowType == DataControlRowType.Footer)
    {

      // Get the OrderTotalTotal Label control in the footer row.
      Label total = (Label)e.Row.FindControl("OrderTotalLabel");

      // Display the grand total of the order formatted as currency.
      if (total != null)
      {
        total.Text = orderTotal.ToString("c");
      }

    }

  }

  void OrderGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    // Retrieve the current row. 
    GridViewRow row = e.Row;

    // Add the field value to the column total if the row being created is
    // a data row. 
    if (row.RowType == DataControlRowType.DataRow)
    {

      // Get the cell that contains the item total.
      TableCell cell = e.Row.Cells[2];

      // Get the DataBoundLiteralControl control that contains the 
      // data bound value.
      DataBoundLiteralControl boundControl = (DataBoundLiteralControl)cell.Controls[0];

      // Remove the '$' character for the type converter to work properly.
      String itemTotal = boundControl.Text.Replace("$",  "");

      // Add the total for an item (row) to the order total.
      orderTotal += Convert.ToDecimal(itemTotal);

    }

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewRow RowType Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridViewRow RowType Example</h3>

      <!-- Populate the Columns collection declaratively. -->
      <!-- Create a custom TemplateField column that uses      -->
      <!-- two Label controls to display an author's first and -->
      <!-- last name in the same column.                       -->
      <asp:gridview id="OrderGridView" 
        datasourceid="OrderSqlDataSource" 
        autogeneratecolumns="False" 
        showfooter="true"
        onrowcreated="OrderGridView_RowCreated"
        onrowdatabound="OrderGridView_RowDataBound"   
        runat="server">

        <columns>

          <asp:boundfield datafield="UnitPrice"
            itemstyle-horizontalalign="Right"
            headertext="Unit Price" 
            dataformatstring="{0:c}"/>

          <asp:boundfield datafield="Quantity"
            itemstyle-horizontalalign="Right"
            headertext="Quantity"/>

          <asp:templatefield headertext="Total"
            itemstyle-horizontalalign="Right"
            footerstyle-horizontalalign="Right"
            footerstyle-backcolor="Blue"
            footerstyle-forecolor="White">
            <itemtemplate>
              <%#Eval("Total", "{0:c}") %>
            </itemtemplate>
            <footertemplate>
              <asp:label id="OrderTotalLabel"
                runat="server"/>
            </footertemplate>
          </asp:templatefield>

        </columns>

      </asp:gridview>

      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Northwind sample database.                   -->
      <asp:sqldatasource id="OrderSqlDataSource"  
        selectcommand="SELECT [OrderID], [UnitPrice], [Quantity], [UnitPrice]*[Quantity] As [Total] FROM [order details] WHERE [OrderID]=10248"
        connectionstring="server=localhost;database=northwind;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft