GridView.RowDataBound 事件

本文详细介绍了.NET Framework中的GridView控件RowDataBound事件,包括其用途、触发时机及如何利用该事件来修改绑定到GridView的数据。通过示例代码展示了如何在数据行绑定前修改公司名称的显示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.NET Framework 类库 
GridView.RowDataBound 事件 

注意:此事件在 .NET Framework 2.0 版中是新增的。

GridView 控件中将数据行绑定到数据时发生。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

语法语法
Visual Basic(声明)
Public Event RowDataBound As GridViewRowEventHandler
Visual Basic(用法)
Dim instance As GridView
Dim handler As GridViewRowEventHandler

AddHandler instance.RowDataBound, handler
C#
public event GridViewRowEventHandler RowDataBound
C++
public:
event GridViewRowEventHandler^ RowDataBound {
    void add (GridViewRowEventHandler^ value);
    void remove (GridViewRowEventHandler^ value);
}
J#
/** @event */
public void add_RowDataBound (GridViewRowEventHandler value)

/** @event */
public void remove_RowDataBound (GridViewRowEventHandler value)
JScript
JScript 支持使用事件,但不支持进行新的声明。
备注备注

呈现 GridView 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 GridViewRow 对象表示)绑定到 GridView 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如修改绑定到该行的数据的值)。

GridViewRowEventArgs 对象将被传给事件处理方法,以便您可以访问正在绑定的行的属性。若要访问行中的特定单元格,请使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在绑定的是哪一种行类型(标题行、数据行等等)。

有关处理事件的更多信息,请参见 使用事件

示例示例

下面的代码示例演示如何使用 RowDataBound 事件在数据源中的字段值显示在 GridView 控件中之前修改该值。

Visual Basic
<%@ Page language="VB" %>

<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>
  <body>
    <form 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="C#" %>

<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>
  <body>
    <form 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>

     /// <summary>
    /// 在 GridView 控件中将数据行绑定到数据时发生
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void dv_Result_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //删除确认
            e.Row.Cells[12].Attributes.Add("onclick", "javascript:return confirm('是否确认删除?');");
            //判断是否具有修改,审批和删除权限
            string UserID = (string)Session["UserID"];
            if (this.dv_Result.DataKeys[e.Row.RowIndex]["NextcheckPersonID"] != UserID)
            {
                //修改
                e.Row.Cells[5].Enabled = false;
                //审批
                e.Row.Cells[10].Enabled = false;
            }
            if (this.dv_Result.DataKeys[e.Row.RowIndex]["establishPersonID"] != UserID)
            {
                //删除
                e.Row.Cells[12].Enabled = false;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值