自己在工作中写的一个GridViewHandler类,封装了创建多行表头和生成邮件文本的功能,希望大家以后有用,附代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
namespace Wjs
{
public class GridViewHandler
{
#region -- 多行表头 --
/// <summary>
///
/// </summary>
/// <param name="text">标题文本</param>
/// <param name="rowSpan">跨行数</param>
/// <param name="colSpan">跨列数</param>
/// <param name="isRowTail">是否是换行单元格(前一行的行尾)</param>
/// <returns></returns>
protected TableHeaderCell DefineTableHeaderCell(string text, int rowSpan, int colSpan, bool isRowTail)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = isRowTail ? text + "</th></tr><tr>" : text;
cell.Attributes.Add("rowspan", rowSpan.ToString());
cell.Attributes.Add("colspan", colSpan.ToString());
cell.Attributes.Add("style", "font-size:10pt;font-weight:bold");
return cell;
}
/// <summary>
/// 多行表头,在GridView的RowCreate事件中使用
/// </summary>
/// <param name="Row"></param>
protected void ReDefineHeaderRow(GridViewRow Row)
{
if (Row.RowType == DataControlRowType.Header)
{
//第一行表头
TableCellCollection tcHeader = Row.Cells;
tcHeader.Clear();
tcHeader.Add(DefineTableHeaderCell("差异判断", 2, 1, false));
tcHeader.Add(DefineTableHeaderCell("银行名称", 2, 1, false));
tcHeader.Add(DefineTableHeaderCell("银行进款汇总", 1, 2, false));
tcHeader.Add(DefineTableHeaderCell("控台交易汇总", 1, 2, false));
tcHeader.Add(DefineTableHeaderCell("当日差异", 2, 1, false));
tcHeader.Add(DefineTableHeaderCell("差异原因汇总", 1, 3, false));
tcHeader.Add(DefineTableHeaderCell("累计差异", 2, 1, false));
tcHeader.Add(DefineTableHeaderCell("处理状态", 2, 1, false));
tcHeader.Add(DefineTableHeaderCell("操作", 2, 1, true));
tcHeader.Add(DefineTableHeaderCell("笔数", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("金额", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("笔数", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("金额", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("类型", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("金额", 1, 1, false));
tcHeader.Add(DefineTableHeaderCell("备注", 1, 1, false));
}
}
#endregion
/// <summary>
/// 包含模板列的GridView无法输出html,要现把它复制到其他的gv中
/// </summary>
/// <param name="model">需要以邮件发送的gv</param>
/// <param name="list">gv的数据源</param>
/// <returns></returns>
private GridView CopyGridView(GridView model, object list)
{
GridView gvTmp = new GridView();
gvTmp.Width = Unit.Percentage(97);
gvTmp.CellPadding = 4;
gvTmp.AutoGenerateColumns = false;
gvTmp.HorizontalAlign = HorizontalAlign.Center;
for (int i = 0; i < model.Columns.Count; i++)
{
DataControlField cell = model.Columns[i];
cell.ItemStyle.Font.Size = 9;
gvTmp.Columns.Add(cell as BoundField);
}
gvTmp.DataSource = list;
gvTmp.DataBind();
return gvTmp;
}
/// <summary>
/// 生成邮件内容(html)
/// </summary>
/// <param name="model">需要以邮件发送的gv</param>
/// <param name="title">邮件中表段的Title</param>
/// <param name="list">gv的数据源</param>
/// <returns></returns>
public string GetHtmlOfGridView(GridView model, string title, object list)
{
IList lst = list as IList;
StringBuilder sb = new StringBuilder();
if (!string.IsNullOrEmpty(title))
{
sb.AppendLine("<h5 >" + title + "</h5>");
}
if (lst.Count == 0)
{
sb.AppendLine("<div style=\"font-size: 13px;align:Center; font-weight: bold; \" align=\"center\">无记录!</div>");
return sb.ToString();
}
string content = "";
GridView gvTmp = CopyGridView(model, list);
StringWriter sw = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(sw);
gvTmp.RenderControl(writer);
writer.Close();
content = sw.ToString();
sb.AppendLine(content);
sb.AppendLine("<br/>");
return sb.ToString();
}
}
}