< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="False" OnRowDataBound ="GridView1_RowDataBound" > < Columns > < asp:BoundField DataField ="医生" HeaderText ="医生" /> </ Columns > </ asp:GridView > using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class admin_Default : System.Web.UI.Page ... { static SqlDataReader dr; protected void Page_Load(object sender, EventArgs e) ...{ SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString); SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString); string[] a = new string[7]; for (int i = 0; i < 7; i++) ...{ a[i] = date(System.DateTime.Now.AddDays(i)); } string sql = " select 医生," + a[0] + "," + a[1] + "," + a[2] + "," + a[3] + "," + a[4] + "," + a[5] + "," + a[6] + " from test"; SqlCommand com = con1.CreateCommand(); com.CommandText = sql; con1.Open(); dr = com.ExecuteReader(); SqlDataAdapter da = new SqlDataAdapter(sql,con); DataSet ds = new DataSet(); da.Fill(ds); TemplateField customField = new TemplateField(); customField.ShowHeader = true; customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[0],1); customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",1); GridView1.Columns.Add(customField); TemplateField customField1 = new TemplateField(); customField1.ShowHeader = true; customField1.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[1], 2); customField1.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",2); GridView1.Columns.Add(customField1); TemplateField customField2 = new TemplateField(); customField2.ShowHeader = true; customField2.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[2], 3); customField2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",3); GridView1.Columns.Add(customField2); TemplateField customField3 = new TemplateField(); customField3.ShowHeader = true; customField3.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[3], 4); customField3.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",4); GridView1.Columns.Add(customField3); TemplateField customField4 = new TemplateField(); customField4.ShowHeader = true; customField4.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[4], 5); customField4.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",5); GridView1.Columns.Add(customField4); TemplateField customField5 = new TemplateField(); customField5.ShowHeader = true; customField5.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[5], 6); customField5.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",6); GridView1.Columns.Add(customField5); TemplateField customField6 = new TemplateField(); customField6.ShowHeader = true; customField6.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, a[6], 7); customField6.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "",7); GridView1.Columns.Add(customField6); GridView1.DataSource = ds; GridView1.DataBind(); con1.Close(); } private string date(DateTime d) ...{ string str = d.DayOfWeek.ToString(); string dates = ""; switch (str) ...{ case "Monday": dates = "星期一"; break; case "Tuesday": dates = "星期二"; break; case "Wednesday": dates = "星期三"; break; case "Thursday": dates = "星期四"; break; case "Friday": dates = "星期五"; break; case "Saturday": dates = "星期六"; break; case "Sunday": dates = "星期日"; break; } return dates; } public class GridViewTemplate : ITemplate ...{ private DataControlRowType templateType; private string columnName; private int tid; public GridViewTemplate(DataControlRowType type, string colname,int id) ...{ templateType = type; columnName = colname; tid = id; } public void InstantiateIn(System.Web.UI.Control container) ...{ switch (templateType) ...{ case DataControlRowType.Header: Literal lc = new Literal(); lc.Text = columnName; container.Controls.Add(lc); break; case DataControlRowType.DataRow: HyperLink HyperLink = new HyperLink(); HyperLink.ID = "HyperLink" + tid.ToString(); container.Controls.Add(HyperLink); break; default: break; } } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) ...{ if (e.Row.RowType == DataControlRowType.DataRow) ...{ if (dr.Read()) ...{ HyperLink hy1 = (HyperLink)e.Row.FindControl("HyperLink1"); hy1.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now) +"&name=" + dr[0].ToString() + ""; hy1.Text = dr[1].ToString(); HyperLink hy2 = (HyperLink)e.Row.FindControl("HyperLink2"); hy2.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(1)) + "&name=" + dr[0].ToString() + ""; hy2.Text = dr[2].ToString(); HyperLink hy3 = (HyperLink)e.Row.FindControl("HyperLink3"); hy3.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(2)) + "&name=" + dr[0].ToString() + ""; hy3.Text = dr[3].ToString(); HyperLink hy4 = (HyperLink)e.Row.FindControl("HyperLink4"); hy4.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(3)) + "&name=" + dr[0].ToString() + ""; hy4.Text = dr[4].ToString(); HyperLink hy5 = (HyperLink)e.Row.FindControl("HyperLink5"); hy5.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(4)) + "&name=" + dr[0].ToString() + ""; hy5.Text = dr[5].ToString(); HyperLink hy6 = (HyperLink)e.Row.FindControl("HyperLink6"); hy6.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(5)) + "&name=" + dr[0].ToString() + ""; hy6.Text = dr[6].ToString(); HyperLink hy7 = (HyperLink)e.Row.FindControl("HyperLink7"); hy7.NavigateUrl = "xxx.aspx?time=" + date(System.DateTime.Now.AddDays(6)) + "&name=" + dr[0].ToString() + ""; hy7.Text = dr[7].ToString(); } } }}