DataControlRowType 枚举

定义

指定数据控件(例如 DetailsViewGridView 控件)中行的功能。

public enum class DataControlRowType
public enum DataControlRowType
type DataControlRowType = 
Public Enum DataControlRowType
继承
DataControlRowType

字段

DataRow 2

数据控件的数据行。 只有 DataRow 行能绑定数据。

EmptyDataRow 5

数据绑定控件的空行。 当数据绑定控件没有可显示的记录并且 EmptyDataTemplate 模板不为 null 时显示空行。

1

数据控件的脚注行。 脚注行不能绑定数据。

Header 0

数据控件的标题行。 标题行不能绑定数据。

Pager 4

显示页导航按钮或页导航控件的行。 页导航行不能绑定数据。

Separator 3

行分隔符。 行分隔符不能绑定数据。

示例

下面的代码示例演示如何在处理GridView控件时使用DataControlRowType枚举检查行的类型。 该方法AuthorsGridView_RowCreated确保CommandArgument控件的属性对于控件中的所有GridView数据行是唯一LinkButton的,以便单击行的LinkButton控件时,可以正确识别该控件。


<%@ 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 Page_Load(Object sender, EventArgs e)
  {

    // Create a new GridView object.
    GridView customersGridView = new GridView();

    // Set the GridView object's properties.
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSource";
    customersGridView.AutoGenerateColumns = false;

    // Dynamically create the columns for the GridView control.
    ButtonField addColumn = new ButtonField();
    addColumn.CommandName = "Add";
    addColumn.Text = "Add";
    addColumn.ButtonType = ButtonType.Link;

    BoundField companyNameColumn = new BoundField();
    companyNameColumn.DataField = "CompanyName";
    companyNameColumn.HeaderText = "Company Name";

    BoundField cityColumn = new BoundField();
    cityColumn.DataField = "City";
    cityColumn.HeaderText = "City";

    // Add the columns to the Columns collection
    // of the GridView control.
    customersGridView.Columns.Add(addColumn);
    customersGridView.Columns.Add(companyNameColumn);
    customersGridView.Columns.Add(cityColumn);

    // Programmatically register the event handling methods.
    customersGridView.RowCommand += new GridViewCommandEventHandler(this.CustomersGridView_RowCommand);
    customersGridView.RowCreated += new GridViewRowEventHandler(this.CustomersGridView_RowCreated);

    // Add the GridView object to the Controls collection
    // of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView);

  }

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    // If multiple ButtonField columns are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button clicked
      // by the user from the Rows collection. Use the
      // CommandSource property to access the GridView control.
      GridView customersGridView = (GridView)e.CommandSource;
      GridViewRow row = customersGridView.Rows[index];

      // Create a new ListItem object for the customer in the row.
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[1].Text) + " " + Server.HtmlDecode(row.Cells[2].Text);

      // If the author is not already in the ListBox, add the ListItem
      // object to the Items collection of a ListBox control.
      if(!CustomersListBox.Items.Contains(item))
      {
        CustomersListBox.Items.Add(item);
      }
    }
  }

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
  {

    // The GridViewCommandEventArgs class does not contain a
    // property that indicates which row's command button was
    // clicked. To identify which row was clicked, use the button's
    // CommandArgument property by setting it to the row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];

      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();
    }

  }

</script>

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

      <h3>GridViewCommandEventArgs Example</h3>

      <table width="100%">
        <tr>
          <td style="width:50%">
            <asp:placeholder id="GridViewPlaceHolder"
              runat="Server"/>
          </td>

          <td style="vertical-align:top; width:50%">
             Customers: <br/>
             <asp:listbox id="CustomersListBox"
               runat="server"/>
          </td>
        </tr>
      </table>

      <!-- 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], [City] 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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a new GridView object.
    Dim customersGridView As New GridView()

    ' Set the GridView object's properties.
    customersGridView.ID = "CustomersGridView"
    customersGridView.DataSourceID = "CustomersSource"
    customersGridView.AutoGenerateColumns = False

    ' Dynamically create the columns for the GridView control.
    Dim addColumn As New ButtonField()
    addColumn.CommandName = "Add"
    addColumn.Text = "Add"
    addColumn.ButtonType = ButtonType.Link

    Dim companyNameColumn As New BoundField()
    companyNameColumn.DataField = "CompanyName"
    companyNameColumn.HeaderText = "Company Name"

    Dim cityColumn As New BoundField()
    cityColumn.DataField = "City"
    cityColumn.HeaderText = "City"

    ' Add the columns to the Columns collection
    ' of the GridView control.
    customersGridView.Columns.Add(addColumn)
    customersGridView.Columns.Add(companyNameColumn)
    customersGridView.Columns.Add(cityColumn)

    ' Programmatically register the event handling methods.
    AddHandler customersGridView.RowCommand, AddressOf CustomersGridView_RowCommand
    AddHandler customersGridView.RowCreated, AddressOf CustomersGridView_RowCreated

    ' Add the GridView object to the Controls collection
    ' of the PlaceHolder control.
    GridViewPlaceHolder.Controls.Add(customersGridView)

  End Sub

  Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
  
    ' If multiple ButtonField columns are used, use the
    ' CommandName property to determine which button was clicked.
    If e.CommandName = "Add" Then
    
      ' Convert the row index stored in the CommandArgument
      ' property to an Integer.
      Dim index As Integer = Convert.ToInt32(e.CommandArgument)

      ' Retrieve the row that contains the button clicked
      ' by the user from the Rows collection. Use the
      ' CommandSource property to access the GridView control.
      Dim customersGridView As GridView = CType(e.CommandSource, GridView)
      Dim row As GridViewRow = customersGridView.Rows(index)

      ' Create a new ListItem object for the customer in the row.
      Dim item As New ListItem()
      item.Text = Server.HtmlDecode(row.Cells(1).Text) + " " + Server.HtmlDecode(row.Cells(2).Text)

      ' If the author is not already in the ListBox, add the ListItem
      ' object to the Items collection of a ListBox control.
      If Not CustomersListBox.Items.Contains(item) Then
      
        CustomersListBox.Items.Add(item)
        
      End If
    End If
        
  End Sub

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    ' The GridViewCommandEventArgs class does not contain a
    ' property that indicates which row's command button was
    ' clicked. To identify which row was clicked, use the button's
    ' CommandArgument property by setting it to the row's index.
    If e.Row.RowType = DataControlRowType.DataRow Then

      ' Retrieve the LinkButton control from the first column.
      Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

      ' Set the LinkButton's CommandArgument property with the
      ' row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString()
      
    End If

  End Sub

</script>

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

      <h3>GridViewCommandEventArgs Example</h3>

      <table width="100%">
        <tr>
          <td style="width:50%">
            <asp:placeholder id="GridViewPlaceHolder"
              runat="Server"/>
          </td>

          <td style="vertical-align:top; width:50%">
             Customers: <br/>
             <asp:listbox id="CustomersListBox"
               runat="server"/>
          </td>
        </tr>
      </table>

      <!-- 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], [City] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>

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

注解

DataControlRowType枚举标识数据控件中行的函数。 它由 DetailsView 控件 GridView 用来区分显示数据和显示其他用户界面的行 (UI) 元素(如标题行、行分隔符或寻呼按钮)的行。

在枚举通过或集合时,可以使用DataControlRowType枚举来标识或DetailsViewRowDetailsViewRowCollection对象的类型GridViewRowGridViewRowCollection 如果要编写创建行的数据控件,可以使用 DataControlRowType 枚举来标识控件中不同行的函数。

适用于

另请参阅