找了很久才找到的在.NET中生成二維表格的代碼,不敢獨享,現在就貼出來給大家看看,相信對大家有所幫助。
代碼如下:
void Page_Load(object o, EventArgs e)
...{
DataTable dt = GetData(); //assume GetData returns the DataTable
//probably better to use Hashtable for depts and months too, but to keep the order, let’s use ArrayList
string sSeparator = ":";
ArrayList alDept = new ArrayList(); //種類
ArrayList alMonth = new ArrayList(); //代碼
Hashtable ht = new Hashtable();
Webjx.Com
foreach (DataRow dr in dt.Rows)
...{
string sDept = dr["c"].ToString();
string sMonth2 = dr["p"].ToString();
//將產地代碼轉換為產地名稱
string sMonth = GetData2(sMonth2);
if (!alDept.Contains(sDept))
alDept.Add(sDept);
if (!alMonth.Contains(sMonth))
alMonth.Add(sMonth);
ht[sDept+ sSeparator + sMonth] = dr["a"];
}
TableRow tr = new TableRow();
TableCell tc = new TableCell();
//tc.Text = " ";
//tr.Cells.Add(tc);
foreach (string sDept in alDept)
...{
int i=0; //用於計算某一種類的數量
foreach (string sMonth in alMonth)
...{
if(ht[sDept+ sSeparator + sMonth]==null)
...{
i=i+0;
}
else
...{
i = i + int.Parse(ht[sDept+ sSeparator + sMonth].ToString());
}
}
tc = new TableCell();
tc.Text= sDept+"("+i+")";
tr.Cells.Add(tc);
}
/**//*foreach (string sDept in alDept)
{
tc = new TableCell();
tc.Text= sDept;
tr.Cells.Add(tc);
} */
Table1.Rows.Add(tr);
foreach (string sMonth in alMonth)
...{
tr = new TableRow();
/**//*tc = new TableCell();
tc.Text = sMonth;
tr.Cells.Add(tc);*/
foreach (string sDept in alDept)
...{
tc = new TableCell();
if(ht[sDept+ sSeparator + sMonth]==null)
...{
tc.Text=sMonth+"(0)";
}
else
...{
tc.Text = sMonth+"("+ ht[sDept+ sSeparator + sMonth].ToString()+")";
}
tr.Cells.Add(tc);
}
Table1.Rows.Add(tr);
}
}
Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**////
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
///
private void InitializeComponent()
...{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public DataTable GetData()
...{
StatisticsB stat=new StatisticsB();
DataSet dataset=stat.byStone();
return dataset.Tables["stat"];
}
//取得名稱列表
public string GetData2(string statid)
...{
StatisticsB stat=new StatisticsB();
return stat.changeToName(statid);
}