读书人

GridView的RowDataBound事件,该怎么处

发布时间: 2012-02-02 23:57:14 作者: rapoo

GridView的RowDataBound事件

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

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

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

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


[解决办法]
将单元格的字体变为斜体显示
[解决办法]
???
[解决办法]
不理解!

我在灌水么
<哥们,请不要恶意灌水>
[解决办法]
心沉闷。没心情给你看这么多代码
不过给你顶下
[解决办法]
教学贴?
[解决办法]
?没看明白问题是什么
[解决办法]
参考:

http://blog.csdn.net/insus/archive/2008/03/26/2221251.aspx

读书人网 >asp.net

热点推荐