asp.net 2.0常见问题技巧1
2013-07-01 17:24
309 查看
常见的一个应用场景,就是gridview中,当库存量少于某个数时,背景颜色先变色
还有就是对某一列统计其总和,显示在页脚里,下面分别阐述之
首先是当库存小于某个值时,行的背景颜色改变,比如
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 确认"库存量"字段的值。
//
// 我们透过一个 DataBinder.Eval() 调用从将被绑定至 GridView 数据列的
// 数据中取得"库存量"字段的值,传递给 DataBinder.Eval() 的第一个参
// 数是将被绑定至 GridView 数据列的数据(也就是 e.Row.DataItem),
// 传递给 DataBinder.Eval() 的第二个参数则是字段名称。
decimal stock =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "库存量"));
if (stock <= 0)
{
// 如果库存量小于或等于 0,则将该资料列的背景色设定成红色。
e.Row.BackColor = Color.Red;
}
decimal totalMoney =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额"));
if (totalMoney > 0)
{
// 如果订货金额大于 0,则将该资料列的背景色设定成***。
e.Row.BackColor = Color.Yellow;
}
// 累加订货金额并设定给变量 orderTotal。
orderTotal += totalMoney;
}
}
而在页面中加入footer模版
<asp:TemplateField HeaderText="订货金额" SortExpression="订货金额">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
// 创建一个变量来存储订货金额加总。
private decimal orderTotal = 0.0m;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// 提取当前的资料列。
GridViewRow row = e.Row;
// 如果正被创建的数据列是一个页尾,则更新数据行加总。
if (row.RowType == DataControlRowType.Footer)
{
// 取得页尾当中的标签控件 OrderTotalTotal 。
Label total = (Label)(e.Row.FindControl("OrderTotalLabel"));
// 以货币格式来显示订货金额加总。
if (total != null)
{
total.Text = orderTotal.ToString("c");
}
}
}
还有就是对某一列统计其总和,显示在页脚里,下面分别阐述之
首先是当库存小于某个值时,行的背景颜色改变,比如
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// 确认"库存量"字段的值。
//
// 我们透过一个 DataBinder.Eval() 调用从将被绑定至 GridView 数据列的
// 数据中取得"库存量"字段的值,传递给 DataBinder.Eval() 的第一个参
// 数是将被绑定至 GridView 数据列的数据(也就是 e.Row.DataItem),
// 传递给 DataBinder.Eval() 的第二个参数则是字段名称。
decimal stock =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "库存量"));
if (stock <= 0)
{
// 如果库存量小于或等于 0,则将该资料列的背景色设定成红色。
e.Row.BackColor = Color.Red;
}
decimal totalMoney =
Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "订货金额"));
if (totalMoney > 0)
{
// 如果订货金额大于 0,则将该资料列的背景色设定成***。
e.Row.BackColor = Color.Yellow;
}
// 累加订货金额并设定给变量 orderTotal。
orderTotal += totalMoney;
}
}
而在页面中加入footer模版
<asp:TemplateField HeaderText="订货金额" SortExpression="订货金额">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("订货金额", "{0:c}") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="OrderTotalLabel" runat="server" Font-Underline="True" ForeColor="Red"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("订货金额", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
// 创建一个变量来存储订货金额加总。
private decimal orderTotal = 0.0m;
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// 提取当前的资料列。
GridViewRow row = e.Row;
// 如果正被创建的数据列是一个页尾,则更新数据行加总。
if (row.RowType == DataControlRowType.Footer)
{
// 取得页尾当中的标签控件 OrderTotalTotal 。
Label total = (Label)(e.Row.FindControl("OrderTotalLabel"));
// 以货币格式来显示订货金额加总。
if (total != null)
{
total.Text = orderTotal.ToString("c");
}
}
}
相关文章推荐
- asp.net 2.0常见问题技巧1
- asp.net 2.0常见问题技巧1
- 转载: asp.net ajax UpdatePanel使用技巧以及常见问题
- 常见的 ASP.NET 2.0 转换问题和解决方案
- 常见的 ASP.NET 2.0 转换问题和解决方案
- 解决 web服务器部署常见问题,server application unavailable 和 程序无法连接数据库 的问题(asp.net 2.0 + oracle9i + winXP)
- 解决 web服务器部署常见问题,server application unavailable 和 程序无法连接数据库 的问题(asp.net 2.0 + oracle9i + winXP)
- asp.net ajax UpdatePanel使用技巧以及常见问题
- ASP.NET 2.0 绑定高级技巧
- Asp.net 2.0自定义控件(点击HyperLink后执行事件)[网友问题: DataList里HyperLink控件激发事件,在哪定义?]
- ASP.NET 2.0 绑定高级技巧
- ASP.NET 2.0中Gridview控件高级技巧
- [ASP.NET] 将数据绑定到DropDownlist中的常见问题
- Asp.net 2.0自定义控件(点击HyperLink后执行事件)[网友问题: DataList里HyperLink控件激发事件,在哪定义?]
- ASP.NET WebAPI 集成 Swagger 启用 OAuth 2.0 配置问题
- asp.net部署到IIS常见问题的解决方法
- ASP.NET 2.0中CSS失效的问题总结(摘自孟子E章)
- asp.net Forums2.0修改密码后无法登陆问题——都是Cache惹的祸
- ASP.NET 2.0中母版页中引用文件路径的问题(对原处理方法中的bug进行了修改)
- ASP.net 2.0中水晶报表迁移部署问题