您的位置:首页 > 其它

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

2011-11-25 09:11 417 查看
效果图如下:



代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="WebApplication1.Demo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Tim Demo 演示</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:GridView ID="GridView1" runat="server" 
			onrowdatabound="GridView1_RowDataBound">
		</asp:GridView>
    </div>
    </form>
</body>
</html>


后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;

namespace WebApplication1
{
	public partial class Demo : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindData();
			}
		}

		void BindData()
		{ 
			var list = new List<Temp>
			{
				new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="4.10", TOC="6.83",pH="7.10"},
				new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="4.2", TOC="6.34",pH="7.3"},
				new Temp{ SiteName="千灯浦口",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-2.38%", TOC="7.73%",pH="-2.74%"},
				new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="3.9", TOC="6.38",pH="7.18"},
				new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="5.1", TOC="6.08",pH="6.99"},
				new Temp{ SiteName="石浦大桥",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-23.53%", TOC="4.93%",pH="2.72%"},
				new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.自动值.ToString(), CODMn="3.30", TOC="6.43",pH="6.64"},
				new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.手工值.ToString(), CODMn="3.5", TOC="5.88",pH="7.02"},
				new Temp{ SiteName="朱军港口",Time="2011-9-23",Function=Fun.偏差.ToString(), CODMn="-5.71%", TOC="9.35%",pH="-5.41%"}
			};
			GridView1.DataSource = list;
			GridView1.DataBind();
		}
		enum Fun
		{
			自动值,
			手工值,
			偏差
		};

		string _tempvalue = "";
		int _temprowspan = 1;
		TableCell _temptablecell = null;
		TableCell _temptablecell2 = null;

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Footer)
			{
				if (e.Row.Cells[0].Text == _tempvalue)
				{
					_temprowspan++;
					e.Row.Cells.Remove(e.Row.Cells[0]);
					e.Row.Cells.Remove(e.Row.Cells[0]);
				}
				else
				{
					if (_temprowspan != 1)
					{
						_temptablecell.RowSpan = _temprowspan;
						_temptablecell2.RowSpan = _temprowspan;
					}
					_tempvalue = e.Row.Cells[0].Text;
					_temptablecell = e.Row.Cells[0];
					_temptablecell2 = e.Row.Cells[1];
					_temprowspan = 1;
				}
				if (e.Row.Cells[3].Text == "7.3")
				{
					e.Row.Cells[3].BackColor = Color.Red;
				}
			}
		}
	}
	class Temp
	{
		public string SiteName { get; set; }
		public string Time { get; set; }
		public string Function { get; set; }
		public string CODMn { get; set; }
		public string TOC { get; set; }
		public string pH { get; set; }
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐