GridView根据值的变化改变行列样式
来源:互联网 发布:石家庄铁路知乎 编辑:程序博客网 时间:2024/06/04 23:25
我看到论坛中有询问关于如何在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> 转自www.chinacs.net 2005-03-18 中文C#技术站
根据某列的值改变其样式最好的方法是在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> 转自www.chinacs.net 2005-03-18 中文C#技术站
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- GridView根据值的变化改变行列样式
- gridview根据值的变化改变行列样式
- GridView使用技巧之:根据绑定的内容改变单元格(或者行)的显示样式
- GridView行列的取值
- 根据GridView某列的值改变颜色
- Gridview用法:根据某列的值改变gridview其他列的属性
- js 改变gridview样式
- 根据绑定的值的不同改变gridview里某一单元格的字体或背景颜色
- 根据数据库字段改变GridView中LinkButton的Text
- VSFTPD 服务器安装配置
- ASP CDO.Message 发送邮件完整实例 By shawl.qiu
- 开源/自由项目 大历史
- 编辑Datagrid控件中的项
- Cygwin下如何安装Perl Module[网淘]
- GridView根据值的变化改变行列样式
- ASP.NET中Datagrid常见错误
- DataGrid中DropDownList 的数据绑定
- za
- 2006.9.11随记-感悟身体的重要性
- 在C#程序中实现插件架构
- 关于DataGrid Web控件的热门问题
- An introduction to C++ Traits
- HTML元素的ID和Name属性的区别