62,244
社区成员




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using System.Text;
namespace Echao.WebPage.Class
{
/// <summary>
/// 处理前台页面中GridView的合并及相关处理
/// </summary>
public class ycGridView
{
/// <summary>
/// 合并GridView中数据相同的行 只要有相同的都会合并
/// </summary>
/// <param name="gridView"></param>
public static void MergeRows(GridView gridView)
{
for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gridView.Rows[rowIndex];
GridViewRow previousRow = gridView.Rows[rowIndex + 1];
for (int i = 0; i < row.Cells.Count; i++)
{
if (row.Cells[i].Text == previousRow.Cells[i].Text)
{
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
}
}
/// <summary>
/// 合并GridView中数据指定列的相同行
/// </summary>
/// <param name="gridView"></param>
/// <param name="cellNum"></param>
public static void GroupRows(GridView gridView, int cellNum)
{
try
{
int i = 0, rowSpanNum = 1;
while (i < gridView.Rows.Count - 1)
{
GridViewRow gvr = gridView.Rows[i];
for (++i; i < gridView.Rows.Count; i++)
{
GridViewRow gvrNext = gridView.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible = true;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == gridView.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
catch
{
}
}
#region 合并单元格 合并某一行的所有列
/// <summary>
/// 合并GridView中某行相同信息的行(单元格)
/// </summary>
/// <param name="GridView1">GridView对象</param>
/// <param name="cellNum">需要合并的行</param>
public static void GroupRow(GridView gridView, int rows)
{
TableCell oldTc = gridView.Rows[rows].Cells[0];
for (int i = 1; i < gridView.Rows[rows].Cells.Count; i++)
{
TableCell tc = gridView.Rows[rows].Cells[i]; //Cells[0]就是你要合并的列
if (oldTc.Text == tc.Text)
{
tc.Visible = false;
if (oldTc.ColumnSpan == 0)
{
oldTc.ColumnSpan = 1;
}
oldTc.ColumnSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldTc = tc;
}
}
}
#endregion
#region 合并单元格 合并一行中的几列
/// <summary>
/// 合并单元格 合并一行中的几列
/// </summary>
/// <param name="gridView">GridView ID</param>
/// <param name="rows">行</param>
/// <param name="sCol">开始列</param>
/// <param name="eCol">结束列</param>
public static void GroupRow(GridView gridView, int rows, int sCol, int eCol)
{
TableCell oldTc = gridView.Rows[rows].Cells[sCol];
for (int i = 1; i < eCol - sCol; i++)
{
TableCell tc = gridView.Rows[rows].Cells[i + sCol]; //Cells[0]就是你要合并的列
tc.Visible = false;
if (oldTc.ColumnSpan == 0)
{
oldTc.ColumnSpan = 1;
}
oldTc.ColumnSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
}
#endregion
#region 合并单元格 合并某一列所有行
/// <summary>
/// 合并GridView中某列相同信息的行(单元格)
/// </summary>
/// <param name="gridView"></param>
/// <param name="cellNum"></param>
public static void GroupCol(GridView gridView, int cols)
{
if (gridView.Rows.Count < 1 || cols > gridView.Rows[0].Cells.Count - 1)
{
return;
}
TableCell oldTc = gridView.Rows[0].Cells[cols];
for (int i = 1; i < gridView.Rows.Count; i++)
{
TableCell tc = gridView.Rows[i].Cells[cols];
if (oldTc.Text == tc.Text)
{
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldTc = tc;
}
}
}
#endregion
#region 合并单元格 合并某一列中的某些行
/// <summary>
/// 合并单元格 合并某一列中的某些行
/// </summary>
/// <param name="gridView">GridView ID</param>
/// <param name="cellNum">列</param>
/// <param name="sRow">开始行</param>
/// <param name="eRow">结束列</param>
public static void GroupCol(GridView gridView, int cols, int sRow, int eRow)
{
if (gridView.Rows.Count < 1 || cols > gridView.Columns.Count - 1)
{
return;
}
TableCell oldTc = gridView.Rows[sRow].Cells[cols];
for (int i = 1; i < eRow - sRow; i++)
{
TableCell tc = gridView.Rows[sRow + i].Cells[cols];
tc.Visible = false;
if (oldTc.RowSpan == 0)
{
oldTc.RowSpan = 1;
}
oldTc.RowSpan++;
oldTc.VerticalAlign = VerticalAlign.Middle;
}
}
#endregion
/// <summary>
/// 将gv导出为excel或word
/// </summary>
/// <param name="gvName">gv控件名称</param>
/// <param name="contentType">导出格式Excel:application/ms-excel Word:application/ms-word</param>
/// <param name="fileName">导出名称</param>
public static void ToExcelOrWord(Control gvName, string contentType, string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(fileName));
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = contentType;
gvName.Page.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter stringWriter = new StringWriter(myCItrad);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
gvName.RenderControl(htmlWriter);
HttpContext.Current.Response.Output.Write(stringWriter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
<%@ 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; }
}
}