请问各位是否有实现动态多栏表头的方法??
请问各位在ASP.NET中怎么实现DataGrid或者其他数据控件动态多栏表头的方法?--------------------编程问答-------------------- 合并下单元格就行了:
http://www.cnblogs.com/singlepine/archive/2005/12/30/282990.html --------------------编程问答-------------------- 给你段源码
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
DataControlRowType elemType=e.Row.RowType;
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow dgi = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
GridViewRow dgi1 = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
Table tb = new Table();
tb = (Table)GridView1.Controls[0];
tb.Rows.Clear();
tb.Rows.AddAt(0, dgi);
tb.Rows.AddAt(1, dgi1);
TableCell tc11 = new TableCell();
TableCell tc12 = new TableCell();
TableCell tc13 = new TableCell();
TableCell tc14 = new TableCell();
TableCell tc20 = new TableCell();
TableCell tc21 = new TableCell();
TableCell tc22 = new TableCell();
TableCell tc23 = new TableCell();
TableCell tc24 = new TableCell();
TableCell tc25 = new TableCell();
TableCell tc26 = new TableCell();
TableCell tc27 = new TableCell();
TableCell tc28 = new TableCell();
TableCell tc29 = new TableCell();
TableCell tc210 = new TableCell();
tc11.Text = "类别";
tc11.HorizontalAlign = HorizontalAlign.Center; /*tc11.RowSpan = 2;*/
dgi.Cells.Add(tc11);
tc12.Text = "温度(%)";
tc12.HorizontalAlign = HorizontalAlign.Center;
tc12.ColumnSpan = 5;
dgi.Cells.Add(tc12);
tc13.Text = "相对湿度(%)";
tc13.HorizontalAlign = HorizontalAlign.Center;
tc13.ColumnSpan = 5;
dgi.Cells.Add(tc13);
tc14.Text = "操作";
tc14.HorizontalAlign = HorizontalAlign.Center;
tc14.RowSpan = 2;
dgi.Cells.Add(tc14);
tc20.Text = "日期";
tc20.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc20);
tc21.Text = "表一";
tc21.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc21);
tc22.Text = "表二";
tc22.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc22);
tc23.Text = "表三";
tc23.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc23);
tc24.Text = "表四";
tc24.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc24);
tc25.Text = "平均温度";
tc25.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc25);
tc26.Text = "表一";
tc26.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc26);
tc27.Text = "表二";
tc27.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc27);
tc28.Text = "表三";
tc28.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc28);
tc29.Text = "表四";
tc29.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc29);
tc210.Text = "平均湿度";
tc210.HorizontalAlign = HorizontalAlign.Center;
dgi1.Cells.Add(tc210);
int nColumn = e.Row.Cells.Count;
for (int i = 0; i < nColumn; i++)
{
e.Row.Cells.RemoveAt(0);
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
GridViewRow dgi2 = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
Table tb = new Table();
tb = (Table)GridView1.Controls[0];
tb.Rows.AddAt(Convert.ToInt32(GridView1.Rows.Count)+3, dgi2);
TableCell tc11 = new TableCell();
TableCell tc12 = new TableCell();
TableCell tc21 = new TableCell();
TableCell tc22 = new TableCell();
tc11.Text = "平均温度";
tc11.ForeColor = Color.Black;
tc11.BackColor = Color.White;
tc11.HorizontalAlign = HorizontalAlign.Center;
dgi2.Cells.Add(tc11);
//计算平均温度
decimal Sum1=0;
decimal Statistic1 = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string temp = GridView1.Rows[i].Cells[5].Text.ToString();
if (temp == "")
temp = "0";
Sum1 += Convert.ToDecimal(temp);
Statistic1 = Sum1 / (i+1);
Statistic1 = decimal.Round(Statistic1, 2);
}
tc12.Text = Statistic1.ToString();
tc12.ForeColor = Color.Black;
tc12.BackColor = Color.White;
tc12.HorizontalAlign = HorizontalAlign.Center;
tc12.ColumnSpan = 5;
dgi2.Cells.Add(tc12);
tc21.Text = "平均湿度";
tc21.ForeColor = Color.Black;
tc21.BackColor = Color.White;
tc21.HorizontalAlign = HorizontalAlign.Center;
dgi2.Cells.Add(tc21);
//计算平均湿度
decimal Sum2 = 0;
decimal Statistic2 = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string temp = GridView1.Rows[i].Cells[10].Text.ToString();
if (temp == "")
temp = "0";
Sum2 += Convert.ToDecimal(temp);
Statistic2 = Sum2 / (i + 1);
Statistic2 = decimal.Round(Statistic2, 2);
}
tc22.Text = Statistic2.ToString();
tc22.ForeColor = Color.Black;
tc22.BackColor = Color.White;
tc22.HorizontalAlign = HorizontalAlign.Center;
tc22.ColumnSpan = 5;
dgi2.Cells.Add(tc22);
int nColumn = e.Row.Cells.Count;
for (int i = 0; i < nColumn; i++)
{
e.Row.Cells.RemoveAt(0);
}
}
} --------------------编程问答-------------------- 希望大家能多提供些参考!!
呵呵! --------------------编程问答-------------------- 呵呵! --------------------编程问答-------------------- http://singlepine.cnblogs.com/archive/2005/11/23/282990.html
补充:.NET技术 , ASP.NET