using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Configuration;
public partial class TestDynamic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
String DBConnStr;
DataSet MyDataSet=new DataSet();
System.Data.SqlClient.SqlDataAdapter DataAdapter=new System.Data.SqlClient.SqlDataAdapter();
DBConnStr = WebConfigurationManager.ConnectionStrings["FireAMMaitainConnectionString"].ConnectionString;
// DBConnStr= System.Configuration.ConfigurationSettings.AppSettings["ConnectString"];
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(DBConnStr);
if (myConnection.State!=ConnectionState.Open)
{
myConnection.Open();
}
System.Data.SqlClient.SqlCommand myCommand = new System.Data.SqlClient.SqlCommand("SelectQuery",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
//添加输入查询参数、赋予值
myCommand.Parameters.Add("@CheckTime",SqlDbType.VarChar);
myCommand.Parameters["@CheckTime"].Value = "2011";
//添加输出参数
// myCommand.Parameters.Add("@Rowcount",SqlDbType.Int);
// myCommand.Parameters["@Rowcount"].Direction=ParameterDirection.Output;
// Label1.Text = Convert.ToString( myCommand.ExecuteNonQuery());
DataAdapter.SelectCommand = myCommand;
if (MyDataSet!=null)
{
DataAdapter.Fill(MyDataSet, "table");
}
Label1.Text = MyDataSet.Tables[0].Rows.Count.ToString();
GridView2.DataSource = MyDataSet;
GridView2.DataBind();
//得到存储过程输出参数
// Label1.Text=myCommand.Parameters["@Rowcount"].Value.ToString();
if (myConnection.State == ConnectionState.Open)
{
myConnection.Close();
}
if (!IsPostBack)
{
GridView1.BorderColor = System.Drawing.Color.Black;
ColumnDALTableAdapters.Mai_ColumnTableAdapter adapter = new ColumnDALTableAdapters.Mai_ColumnTableAdapter();
ColumnDAL.Mai_ColumnDataTable cdt = adapter.GetData();
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
DataColumn column = new DataColumn();
column.ColumnName = "时间";
column.Caption = "00";
dt.Columns.Add(column);
// dt.Columns.Add(new System.Data.DataColumn("时间", typeof(System.String)));
for (int j = 0; j < cdt.Rows.Count; j++)
{
DataColumn column1 = new DataColumn();
column1.ColumnName = cdt.Rows[j]["ColumnName"].ToString();
column1.Caption = cdt.Rows[j]["ColumnCode"].ToString();
dt.Columns.Add(column1);
// dt.Columns.Add(new System.Data.DataColumn(cdt.Rows[j]["ColumnName"].ToString(), typeof(System.String), cdt.Rows[j]["ColumnCode"].ToString()));
}
/* dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
* */
dt.Columns.Add(new System.Data.DataColumn(" ", typeof(System.String)));
ColumnResultDALTableAdapters.Mai_ColumnResultTableAdapter cradapter = new ColumnResultDALTableAdapters.Mai_ColumnResultTableAdapter();
// ColumnResultDAL.Mai_ColumnResultDataTable crdt = cradapter.GetData();
ColumnResultDAL.Mai_ColumnResultDataTable crdt = cradapter.GetDataByDeptTime("0001","2011-6");
// crdt = cradapter
for (int i = 0; i < Convert.ToInt32(this.Label1.Text); i++)
{
int m ;
string checkperson = "";
// System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = MyDataSet.Tables[0].Rows[i]["CheckTime"].ToString();
// dr[0] = crdt.Rows[0]["CheckTime"].ToString();
for ( m = 0; m < cdt.Rows.Count; m++)
{
string deptcode = "0001";
string year = MyDataSet.Tables[0].Rows[i]["CheckTime"].ToString();
crdt = cradapter.GetDataByDeptColumnTime(deptcode, dt.Columns[m + 1].Caption, year);
if (crdt.Rows.Count >= 1)
// if (crdt.Rows[0]["ColumnResult"].ToString() != "")
{
dr[m + 1] = crdt.Rows[0]["ColumnResult"].ToString();
checkperson = crdt.Rows[0]["CheckPerson"].ToString();
}
else
dr[m + 1] = "";
}
dr[m + 1] = checkperson;
// dr[m + 1] = crdt.Rows[0]["CheckPerson"].ToString();
/* dr[0] = "学生" + i.ToString();
dr[1] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
* */
dt.Rows.Add(dr);
}
GridView1.DataSource = new System.Data.DataView(dt);
// GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style", "background:#FFF");
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)//如果是标题行
{
//建立新行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
//设置颜色及字体
rowHeader.BackColor = System.Drawing.Color.White;
rowHeader.Font.Bold = true;
//获得Gridview行对象
TableCellCollection cells = e.Row.Cells;
//隐藏第一列 这是关键,不然第一列多出一列
cells[0].Visible = false;
//隐藏最后一列 2011-06-10
cells[e.Row.Cells.Count - 1].Visible = false;
ColumnFatherDALTableAdapters.Mai_ColumnFatherTableAdapter cadapter = new ColumnFatherDALTableAdapters.Mai_ColumnFatherTableAdapter();
ColumnDALTableAdapters.Mai_ColumnTableAdapter adapter = new ColumnDALTableAdapters.Mai_ColumnTableAdapter();
ColumnFatherDAL.Mai_ColumnFatherDataTable cdt = cadapter.GetData();
TableCell headerCell = new TableCell();
headerCell.Text = "检查时间";
headerCell.RowSpan = 2;
//添加单元格
rowHeader.Cells.Add(headerCell);
for (int k = 0; k < cdt.Rows.Count; k++)
{
headerCell = new TableCell();
headerCell.Text = cdt.Rows[k]["ColumnFatherName"].ToString();
ColumnDAL.Mai_ColumnDataTable dt = adapter.GetDataByFatherCode(cdt.Rows[k]["ColumnFatherCode"].ToString());
// headerCell.RowSpan = 2;
headerCell.ColumnSpan = dt.Rows.Count;
rowHeader.Cells.Add(headerCell);
//设置对齐方式
headerCell.HorizontalAlign = HorizontalAlign.Center;
}
headerCell = new TableCell();
headerCell.Text = "点检人";
headerCell.RowSpan = 2;
rowHeader.Cells.Add(headerCell);
// TableCell headerCell = new TableCell();
/* headerCell.Text = "学生姓名";
//设置合并行数
headerCell.RowSpan = 2;
//添加单元格
rowHeader.Cells.Add(headerCell);
//设置对齐方式
rowHeader.Cells[0].VerticalAlign = VerticalAlign.Middle;
rowHeader.Cells[0].HorizontalAlign = HorizontalAlign.Center;
//以下类似,不多说了
headerCell = new TableCell();
headerCell.Text = "学生成绩";
//设置合并列数 为总单元格数-1
headerCell.ColumnSpan = cells.Count - 1;
headerCell.HorizontalAlign = HorizontalAlign.Center;
//添加单元格
rowHeader.Cells.Add(headerCell);
//显示行
* */
rowHeader.Visible = true;
//把行加在第一行前面
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
}
---------------------------------------存储过程--------------------------------------------------------------------------------------------------
USE [MaintainDB]
GO
/****** Object: StoredProcedure [dbo].[SelectQuery] Script Date: 06/10/2011 13:12:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SelectQuery]
(
@CheckTime nvarchar(50)
)
AS
SET NOCOUNT ON;
SELECT DISTINCT CheckTime
FROM Mai_ColumnResult
WHERE (CheckTime LIKE @CheckTime + '%')