步入 IT 业已经有几年的时间了 , 从最早接触 pb6.0 到现在 .Net 技术 , 计算机技术不论是从硬件还是软件都有巨大的进步 . 而中国程序员总体水平在世界上也是远远落后,其中缺乏完善的体系、必要的交流和程序员个人英雄主义的思想是主要原因 . 前不久在工作中遇到一个关于用 DataGrid 分类显示数据的问题,显示的样式入下图所示 : 希望能为遇到类似问题的朋友提供一个解决方案 , 并掌握类似问题的解决方法 .
问题剖析 :
以上为例 , 每门课程属于不同的类别 , 需要将显示数据的第一项类别进行汇总显示 . 用标准的 DataGrid 是难于实现上述功能的 . 显然需要依靠自身来解决 .
思路 :
归根到底 , 不论何种样式的表格显示 , 表现到前台都是 Html 的 Table 元素 , 因此如果能够在读取数据时动态确定 Html 样式 , 并通过 Div 将 html 生成到前台显示的话 , 就可以控制复杂的显示 . 这里面其实包含了从已有显示的 html 反推到动态 html 生成的过程 .
源代码及注释 :
定义类保存类别名字和数据条数
public class KeyVal
{
private string m_Skey;
private string m_SVal;
public string strKey
{
get
{
return m_Skey;
}
set
{
m_Skey=value;
}
}
public string strVal
{
get
{
return m_SVal;
}
set
{
m_SVal=value;
}
}
public KeyVal()
{}
public KeyVal(string SKey,string SVal)
{
strKey=SKey;
strVal=SVal;
}
}
测试页代码和相关函数
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using Microsoft.Web.UI.WebControls;
using System.Text;
namespace EisWebSite.WebInternet
{
///
1<summary>
2/// ClassCourse 的摘要说明。
3/// </summary>
public class ClassCourse : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList SpecialtyID;
protected System.Web.UI.HtmlControls.HtmlGenericControl MainDiv;
//
#region 页面初始化
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
AppGlobal.CBoxFillSpecialtyData(ref this.SpecialtyID,true);
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </summary>
private void InitializeComponent()
{
this.SpecialtyID.SelectedIndexChanged += new System.EventHandler(this.SpecialtyID_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string CreateOutHtml()
{
//取出类型数目以及名称
DataSet dSet=new DataSet();
dSet=添加自己的获取数据集的函数(灵活设计Sql语句)结果为类型、数目
//AppGlobal.AppSysWebService.ClassCourseTeacherMainFilters(Item);
ArrayList mList=new ArrayList();
foreach(DataRow dRow in dSet.Tables[0].Rows)
{
KeyVal mObj=new KeyVal();
mObj.strKey=dRow[0].ToString();
mObj.strVal=dRow[1].ToString();
mList.Add(mObj);
}
StringBuilder OutHtml=new StringBuilder();
dSet=添加自己的数据集函数.注意数据的排序方式与上同
//AppGlobal.AppSysWebService.ClassCourseTeacherFilters(Item);
//添加固定表头
OutHtml.Append("
1<table "="" "+="" +"id="GRid" align="center" border="1" bordercolor="black" cellpadding="0" cellspacing="0" rules="all" style="word-break:break-all; BORDER-RIGHT:black 1px solid; BORDER-TOP:black 1px solid; BORDER-LEFT:black 1px solid; WIDTH:100%; BORDER-BOTTOM:black 1px solid; BORDER-COLLAPSE:collapse">"
2);
3OutHtml.Append("<table "="" "+="" +"id="AGRid" align="center" border="1" bordercolor="black" cellpadding="0" cellspacing="0" rules="all" style="word-break:break-all;BORDER-RIGHT:black 1px solid; BORDER-TOP:black 1px solid; BORDER-LEFT:black 1px solid; WIDTH:100%; BORDER-BOTTOM:black 1px solid; BORDER-COLLAPSE:collapse">");
4OutHtml.Append("<tr align="center">"+
5"<td style="WIDTH: 87px; HEIGHT: 34px" width="87">类别</td>"+
6"<td style="WIDTH: 253px; HEIGHT: 34px">课程编号</td>"+
7"<td style="WIDTH: 280px; HEIGHT: 34px">课程名称</td>"+
8"<td style="WIDTH: 86px; HEIGHT: 34px">学分</td>"+
9"<td style="WIDTH: 140px; HEIGHT: 34px">"+
10"<table cellpadding="0" cellspacing="0" height="100%" width="100%">"+
11"<tr>"+
12"<td align="center" width="33%"></td>"+
13"<td align="center" width="33%">学期</td>"+
14
15"<td align="center" width="33%"></td>"+
16"</tr>"+
17"<tr>"+
18"<td align="center" width="33%">I</td>"+
19"<td align="center" width="33%">II</td>"+
20"<td align="center" width="33%">III</td>"+
21"</tr>"+
22"</table>"+
23"</td>"+
24"<td style="WIDTH: 86px; HEIGHT: 34px">教师名称</td>"+
25"</tr>");
26OutHtml.Append("</table><table "="" "+="" +"id="bGRid" align="center" border="1" bordercolor="black" cellpadding="0" cellspacing="0" rules="all" style="word-break:break-all;BORDER-RIGHT:black 1px solid; BORDER-TOP:black 1px solid; BORDER-LEFT:black 1px solid; WIDTH:775px; BORDER-BOTTOM:black 1px solid; BORDER-COLLAPSE:collapse">");
27
28string SrcType="";
29string NewType="";
30foreach(DataRow dRow in dSet.Tables[0].Rows)
31{
32OutHtml.Append("<tr align="center" height="24px" style="word-break:break-all;"> ");
33
34NewType=dRow["KeyValue"].ToString();
35
36if (SrcType!=NewType)
37OutHtml.Append("<td keyvalue"].tostring(),mlist).strval+"="" rowspan="+SeachObj(dRow[" style="WIDTH: 80px; HEIGHT: 34px" width="80">"+SeachObj(dRow["KeyValue"].ToString(),mList).strKey+"</td>");
38SrcType=NewType;
39
40OutHtml.Append("<td width="231px">"+dRow["courseID"].ToString()+"</td>");
41OutHtml.Append("<td width="255px">"+dRow["courseName"].ToString()+"</td>");
42OutHtml.Append("<td width="80px">"+dRow["credit"].ToString()+"</td>");
43// OutHtml.Append("<td width="100px">");
44// OutHtml.Append("<table border="1" bordercolor="black" cellpadding="0" cellspacing="0" height="100%" width="110">"+
45// "<tr>");
46switch (Convert.ToInt16(dRow["coursetime"].ToString(),10))
47{
48
49case 1:
50OutHtml.Append("<td width="43px">√"+"</td>");
51OutHtml.Append("<td width="43px"></td>");
52OutHtml.Append("<td width="43px"></td>");
53break;
54case 2:
55OutHtml.Append("<td width="43px"></td>");
56OutHtml.Append("<td width="43px">√"+"</td>");
57OutHtml.Append("<td width="43px"></td>");
58break;
59case 3:
60OutHtml.Append("<td width="43px"></td>");
61OutHtml.Append("<td width="43px"></td>");
62OutHtml.Append("<td width="3px">√"+"</td>");
63break;
64default:
65OutHtml.Append("<td width="43px"></td>");
66OutHtml.Append("<td width="43px"></td>");
67OutHtml.Append("<td width="43px"></td>");
68break;
69}
70// OutHtml.Append("</tr></table>");
71// OutHtml.Append("</td>");
72OutHtml.Append("<td style="word-break:break-all;" width="79px">"+dRow["TName"].ToString()+"</td>");
73OutHtml.Append("</tr>");
74}
75//添加固定表尾部
76OutHtml.Append("</table>");
77OutHtml.Append("</table>
");
//
// DGRid.DataSource=dSet;
// DGRid.DataBind();
return OutHtml.ToString();
}
private KeyVal SeachObj(string strKey, ArrayList mList)
{
for (int i=0;i<=mList.Count-1;i++)
{
if (((KeyVal)mList[i]).strKey==strKey)
return (KeyVal)mList[i];
}
return null;
}
}
}