在 控件中将数据行绑定到数据时发生。
命名空间:
程序集: System.Web(在 System.Web.dll 中)
呈现 控件之前,该控件中的每一行必须绑定到数据源中的一条记录。将某个数据行(用 对象表示)绑定到 控件中的数据以后,将引发 RowDataBound 事件。这使您可以提供一个这样的事件处理方法,即每次发生此事件时就执行一个自定义例程(如修改绑定到该行的数据的值)。
对象将传递给事件处理方法,以便您可以访问正在绑定的行的属性。若要访问行中的特定单元格,请使用 对象的 属性中包含的 对象的 属性。使用 属性可确定正在绑定的是哪一种行类型(标题行、数据行等等)。
有关处理事件的更多信息,请参见。
<%@ 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>
2008-08-14 14:38
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//加入鼠标滑过的高亮效果
if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
{
//当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';");
//当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
//单击行改变行背景颜色
e.Row.Attributes.Add("onclick", "this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';");
e.Row.Attributes.Add("ondblclick", "dbclickevent(" + GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString() + ")");
//e.Row.RowIndex为当前行的索引
//DataKeys[索引].Value[0]
}
}
GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行、列数据的途径。
要获取当前行的某个数据列,我在实践中总结有如下几种方法:
1. Cells[x].Txt。
从列单元格的文本值获取。这种方法简单高率,最为常用,但是功能单纯。此法存在几个缺点:
(1)无法获取到设置了隐藏属性的数据列的值,所取到的值为“”(空)。
(2)只能获取在HTML中定义过的数据列,无法查询数据源中的当前数据行的所有字段列。一般情况下,通过HTML设置GridView的字段列的数量往往小于数据源的实际字段数量,这是因为从业务逻辑的角度看,并不是所有的字段都是要显示在页面上的。当需要使用没有被显示的字段时,此法就不能直接满足需求了。
2. e.Row.Cells[x].FindControl("YourcontrolName")。---返回YourcontrolName
这是在单元格内查找某个服务器控件,从而获得其数据值。这种方式可以操作单元格内的服务器控件。此法一般用于处理模板列中的数据或控件。
3. (DataRowView)e.Row.DataItem.Row.ItemArray[x].ToString()。
此法的核心是e.Row.DataItem,它是GridView的行数据集,为Object类型,将其转化为DataRowView类型后,可以获得更多的操作方法。此数据集表示数据源当前行的全部字段列,ItemArray[x]是当前行全部字段列的数组对象,可以通过索引x获得任意字段值。此法的强悍之处是可以对数据源的全部字段查询。
4. DataBinder.Eval(e.Row.DataItem, "YourDataFieldName").ToString()。
此法仍然基于e.Row.DataItem,其特点是更快捷的获得数据源的任意字段值,因为使用了DataBinder.Eval(),需要注意的是不要写错字段名称。
5. 将e.Row.DataItem转化为自定义类型,实现数据类型化。
例如:
DSRequestTracking.TB_RequestTrackingRow rtrow=(DSRequestTracking.TB_RequestTrackingRow)((DataRowView)e.Row.DataItem).Row;
RequestStatusDropDownList1.SelectedValue = rtrow.StatusID.ToString();
其中DSRequestTracking是通过DSRequestTracking.xsd文件在工程中自定义的强类型DataSet, TB_RequestTrackingRow是VS自动为此强类型生成的创建数据行对象的方法。通过此法,实现了类型化数据的广泛应用。数据类型化的好处很多,显而易见的便是传一个类型数据给方法,取代一大堆参数,另外就是再也不会发生字段名拼写错误的情况。
综述,以上方法各有千秋,恰当择而用之,一定能满足开发过程中的不同需求。