GridView 合并单元格(多列)及特定条件的数据高亮显示实现

效果图如下:

 代码:

[html]  view plain copy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="WebApplication1.Demo" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title>Tim Demo 演示</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.         <asp:GridView ID="GridView1" runat="server"   
  13.             onrowdatabound="GridView1_RowDataBound">  
  14.         </asp:GridView>  
  15.     </div>  
  16.     </form>  
  17. </body>  
  18. </html>  


后台:

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Drawing;  
  9.   
  10. namespace WebApplication1  
  11. {  
  12.     public partial class Demo : System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             if (!IsPostBack)  
  17.             {  
  18.                 BindData();  
  19.             }  
  20.         }  
  21.   
  22.         void BindData()  
  23.         {   
  24.             var list = new List<Temp>  
  25.             {  
  26.                 new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="4.10", TOC="6.83",pH="7.10"},  
  27.                 new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="4.2", TOC="6.34",pH="7.3"},  
  28.                 new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-2.38%", TOC="7.73%",pH="-2.74%"},  
  29.                 new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="3.9", TOC="6.38",pH="7.18"},  
  30.                 new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="5.1", TOC="6.08",pH="6.99"},  
  31.                 new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-23.53%", TOC="4.93%",pH="2.72%"},  
  32.                 new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="3.30", TOC="6.43",pH="6.64"},  
  33.                 new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="3.5", TOC="5.88",pH="7.02"},  
  34.                 new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-5.71%", TOC="9.35%",pH="-5.41%"}  
  35.             };  
  36.             GridView1.DataSource = list;  
  37.             GridView1.DataBind();  
  38.         }  
  39.         enum Fun  
  40.         {  
  41.             自动值,  
  42.             手工值,  
  43.             偏差  
  44.         };  
  45.   
  46.         string _tempvalue = "";  
  47.         int _temprowspan = 1;  
  48.         TableCell _temptablecell = null;  
  49.         TableCell _temptablecell2 = null;  
  50.   
  51.         protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  52.         {  
  53.             if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)  
  54.             {  
  55.                 if (e.Row.Cells[0].Text == _tempvalue)  
  56.                 {  
  57.                     _temprowspan++;  
  58.                     e.Row.Cells.Remove(e.Row.Cells[0]);  
  59.                     e.Row.Cells.Remove(e.Row.Cells[0]);  
  60.                 }  
  61.                 else  
  62.                 {  
  63.                     if (_temprowspan != 1)  
  64.                     {  
  65.                         _temptablecell.RowSpan = _temprowspan;  
  66.                         _temptablecell2.RowSpan = _temprowspan;  
  67.                     }  
  68.                     _tempvalue = e.Row.Cells[0].Text;  
  69.                     _temptablecell = e.Row.Cells[0];  
  70.                     _temptablecell2 = e.Row.Cells[1];  
  71.                     _temprowspan = 1;  
  72.                 }  
  73.                 if (e.Row.Cells[3].Text == "7.3")  
  74.                 {  
  75.                     e.Row.Cells[3].BackColor = Color.Red;  
  76.                 }  
  77.             }  
  78.         }  
  79.     }  
  80.     class Temp  
  81.     {  
  82.         public string SiteName { getset; }  
  83.         public string Time { getset; }  
  84.         public string Function { getset; }  
  85.         public string CODMn { getset; }  
  86.         public string TOC { getset; }  
  87.         public string pH { getset; }  
  88.     }  
  89.   
  90. }  
  91. http://bbs.csdn.net/topics/390584460 grid嵌套
  92. http://bbs.csdn.net/topics/390509830 grid的自定义排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值