您的位置:首页 > Web前端 > HTML

如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能

2004-10-24 22:49 1276 查看
作者:未知 请作者速与本人联系

步入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
{
/// <summary>
/// ClassCourse 的摘要说明。
/// </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);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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("<table cellspacing='0' cellpadding='0' align='center' rules='all' bordercolor='black' border='1'"
+"id='GRid'"+
" 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'>"
);
OutHtml.Append("<table cellspacing='0' cellpadding='0' align='center' rules='all' bordercolor='black' border='1'"
+"id='AGRid'"+
" 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'>");
OutHtml.Append("<tr align='center'>"+
"<td width='87' style='WIDTH: 87px; HEIGHT: 34px'>类别</td>"+
"<td style='WIDTH: 253px; HEIGHT: 34px'>课程编号</td>"+
"<td style='WIDTH: 280px; HEIGHT: 34px'>课程名称</td>"+
"<td style='WIDTH: 86px; HEIGHT: 34px'>学分</td>"+
"<td style='WIDTH: 140px; HEIGHT: 34px' >"+
"<table width='100%' height='100%' cellpadding='0' cellspacing='0'>"+
"<tr>"+
"<td align='center'width='33%' ></td>"+
"<td align='center'width='33%'>学期</td>"+

"<td align='center'width='33%' ></td>"+
"</tr>"+
"<tr>"+
"<td align='center' width='33%'>I</td>"+
"<td align='center' width='33%'>II</td>"+
"<td align='center' width='33%'>III</td>"+
"</tr>"+
"</table>"+
"</td>"+
"<td style='WIDTH: 86px; HEIGHT: 34px'>教师名称</td>"+
"</tr>");
OutHtml.Append("</table><table cellspacing='0' cellpadding='0' align='center' rules='all' bordercolor='black' border='1'"
+"id='bGRid'"+
" 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'>");

string SrcType="";
string NewType="";
foreach(DataRow dRow in dSet.Tables[0].Rows)
{
OutHtml.Append("<tr align='center' height='24px' style='word-break:break-all;'> ");

NewType=dRow["KeyValue"].ToString();

if (SrcType!=NewType)
OutHtml.Append("<td width='80' style='WIDTH: 80px; HEIGHT: 34px' rowspan="+SeachObj(dRow["KeyValue"].ToString(),mList).strVal+">"+SeachObj(dRow["KeyValue"].ToString(),mList).strKey+"</td>");
SrcType=NewType;

OutHtml.Append("<td width=231px >"+dRow["courseID"].ToString()+"</td>");
OutHtml.Append("<td width=255px>"+dRow["courseName"].ToString()+"</td>");
OutHtml.Append("<td width=80px>"+dRow["credit"].ToString()+"</td>");
// OutHtml.Append("<td width=100px>");
// OutHtml.Append("<table width='110' height='100%' cellpadding='0' cellspacing='0' bordercolor='black' border='1'>"+
// "<tr>");
switch (Convert.ToInt16(dRow["coursetime"].ToString(),10))
{

case 1:
OutHtml.Append("<td width=43px>√"+"</td>");
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=43px></td>");
break;
case 2:
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=43px>√"+"</td>");
OutHtml.Append("<td width=43px></td>");
break;
case 3:
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=3px>√"+"</td>");
break;
default:
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=43px></td>");
OutHtml.Append("<td width=43px></td>");
break;
}
// OutHtml.Append("</tr></table>");
// OutHtml.Append("</td>");
OutHtml.Append("<td width=79px style='word-break:break-all;'>"+dRow["TName"].ToString()+"</td>");
OutHtml.Append("</tr>");
}
//添加固定表尾部
OutHtml.Append("</table>");
OutHtml.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;
}

}

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