.Net中生成二维的表格的代码 分享
程序员文章站
2024-02-29 11:43:46
代码如下: 复制代码 代码如下: void page_load(object o, eventargs e) ...{ datatable dt = getdat...
代码如下:
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();
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);
}
复制代码 代码如下:
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();
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);
}