- 热门文章:
- · 调试ASP.NET应用程序的方法和技巧(2)
- · 在ASP中操作HTTP报头方法分析(2)
- · 关于解决商务平台ASP程序的源代码泄漏设想与思考
- · 在ASP中操作HTTP报头方法分析(1)
- · 使用用户名+密码的方式连接SQLSERVER
- · ASP.NET+AJAX解决网页打开等待问题(1)
- · ASP.NET+AJAX解决网页打开等待问题(2)
- · ASP .NET中维持ViewState研究
- · ASP.NET中用healthMonitor属性用法
- · 认识ASP.NET配置文件Web.config
- · 报表制作利器—Reporting Services(1)
- · 报表制作利器—Reporting Services(2)
上一篇:ASP.NET程序员应用程序域须知 >>
GridView根据值的变化改变行列样式
根据某列的值改变其样式最好的方法是在GridView的DataRowBound事件中想办法。在GridView中的行绑定数据后将立即执行DataRowBound事件。DataRowBound事件使用GridViewRowEventargs类作为事件变量。通过事件变量你能够利用GridViewRowEventArgs属性操作已经绑定数据的行。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
}
Row将返回TableRow类中的一个GridViewRow对象。
绑定的Row有几种不同的类型。例如:DataRow, EmptyDataRow, Footer, Header, Pager 和 Separator。通过GridView的RowType属性可以得到当前行的行类型。RowType是一组DataControlRow枚举。
看下面的代码示例,检测GridView列出的行是否为一个标准类型的行。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Do something!
}
}
可以使用Row的Cells属性得到其Cells,它将返回一个TableCellCollection对象。然后通过TableCellCollection索引得到特定的Cells。TableCellcollection索引将返回一个TabelCell对象,对应于Row中的一个Cell:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[0].Text;
}
}
现在你已经明白了如何得到GridView中某行某列的值,那么根据值的变化改变其样式就比较容易了。以下示例使用 Northwind 数据库,通过检测第四列(UnitPrice)的值是否大于10将其颜色改变为红色。
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Decimal.Parse(e.Row.Cells[3].Text) > 10)
e.Row.Cells[3].BackColor = Color.Red;
}
}
</script>
<html XMLns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames="ProductID" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField ReadOnly="True" HeaderText="ProductID" InsertVisible="False" DataField="ProductID"
SortExpression="ProductID" />
<asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName" />
<asp:BoundField HeaderText="QuantityPerUnit" DataField="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField HeaderText="UnitPrice" DataField="UnitPrice" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Alphabetical list of products]"
ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" />
</div>
</form>
</body>
</html>
下一篇:调试ASP.NET应用程序的方法和技巧(2) >>
相关文章:
- · 报表制作利器—Reporting Services(3)
- · ASP.NET 2.0中隐藏listbox的某一项
- · Asp.net(C#)给图片加上水印效果
- · ASP.net随机数应用实例
- · ASP.net随机数应用实例(2)
- · .NET WINFORM中使用本地SMTP SERVICE
- · ASP.net中用axWebBrowser中提交表单
- · 基于ASP.NET的自定义分页显示(1)
- · 用ASP.NET结合XML制作广告管理程序(1)
- · 用ASP.NET结合XML制作广告管理程序(2)
- · 使用ASP.NET程序来管理文件或目录的一种方案
- · 利用DataGrid编辑、修改、删除记录(1)
- · 利用DataGrid编辑、修改、删除记录(2)
- · 捕捉DataGrid的双击事件(C#版本)
- · 利用WebClient和WebRequest类获得网页源代码
- · 如何在域控制器上安装asp.net?
- · ASP.NET四种页面导航方式的比较与选择
- · 在ASP.NET下实现数字和字符相混合的验证码(C#)
- · 在网页中动态的生成一个gif图片(ASP.net)
- · 使用ASP.NET 2.0中的GridView控件(1)
- · 使用ASP.NET 2.0中的GridView控件(2)
- · ASP.NET虚拟主机安全漏洞解决方案(1)
- · ASP.NET虚拟主机安全漏洞解决方案(2)
- · ASP.NET虚拟主机安全漏洞解决方案(3)
- · ASP.NET虚拟主机安全漏洞解决方案(4)
- · 利用ASP.NET 2.0创建自定义Web控件(1)
- · 利用ASP.NET 2.0创建自定义Web控件(2)
- · 利用ASP.NET 2.0创建自定义Web控件(3)
- · 利用ASP.NET 2.0创建自定义Web控件(4)
- · 利用ASP.NET 2.0创建自定义Web控件(5)
- · 在ASP.NET下实现数字和字符相混合的验证码
- · 生成图象验证码函数
- · 在ASP.NET 2.0中使用页面导航控件(1)
- · 在ASP.NET 2.0中使用页面导航控件(2)
- · DataGrid基于Access的快速分页法(1)
- · DataGrid基于Access的快速分页法(2)
- · DataGrid基于Access的快速分页法(3)
- · DataGrid基于Access的快速分页法(4)
