步入IT業(yè)已經(jīng)有幾年的時間了,從最早接觸pb6.0到現(xiàn)在.Net技術(shù),計(jì)算機(jī)技術(shù)不論是從硬件還是軟件都有巨大的進(jìn)步.而中國程序員總體水平在世界上也是遠(yuǎn)遠(yuǎn)落后,其中缺乏完善的體系、必要的交流和程序員個人英雄主義的思想是主要原因.前不久在工作中遇到一個關(guān)于用DataGrid分類顯示數(shù)據(jù)的問題,顯示的樣式入下圖所示: 希望能為遇到類似問題的朋友提供一個解決方案,并掌握類似問題的解決方法.
問題剖析:
以上為例,每門課程屬于不同的類別,需要將顯示數(shù)據(jù)的第一項(xiàng)類別進(jìn)行匯總顯示.用標(biāo)準(zhǔn)的DataGrid是難于實(shí)現(xiàn)上述功能的.顯然需要依靠自身來解決.
思路:
歸根到底,不論何種樣式的表格顯示,表現(xiàn)到前臺都是Html的Table元素,因此如果能夠在讀取數(shù)據(jù)時動態(tài)確定Html樣式,并通過Div將html生成到前臺顯示的話,就可以控制復(fù)雜的顯示.這里面其實(shí)包含了從已有顯示的html反推到動態(tài)html生成的過程.
源代碼及注釋:
定義類保存類別名字和數(shù)據(jù)條數(shù)
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; } }
測試頁代碼和相關(guān)函數(shù)
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 窗體設(shè)計(jì)器生成的代碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內(nèi)容。 /// </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() {
//取出類型數(shù)目以及名稱 DataSet dSet=new DataSet(); dSet=添加自己的獲取數(shù)據(jù)集的函數(shù)(靈活設(shè)計(jì)Sql語句)結(jié)果為類型、數(shù)目
//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=添加自己的數(shù)據(jù)集函數(shù).注意數(shù)據(jù)的排序方式與上同
//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'>學(xué)分</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%'>學(xué)期</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>"+ ""+ "</td>"+ "<td style='WIDTH: 86px; HEIGHT: 34px'>教師名稱</td>"+ "</tr>"); OutHtml.Append("<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>"); // OutHtml.Append("</td>"); OutHtml.Append("<td width=79px style='word-break:break-all;'>"+dRow["TName"].ToString()+"</td>"); OutHtml.Append("</tr>"); } //添加固定表尾部 OutHtml.Append(""); OutHtml.Append(""); // // 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; }
}
}
|