今天想 做了一下 gridview 在没有数据的情况下 也能显示表头呢 虽然 没有封装 还是面向过程的 不过 还是做出来了
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } public void Bind() { string sqlstr = "select [Thesis].Lwid,[Issue].Kttm,[Thesis].LNd,[Thesis].Sci,[Thesis].lwtitle,[Periodical_Type].qklx,[Periodical].Qkmc,[Thesis_Author].Zzxm,[Issue_Groups].Zbmc from [Issue_Thesis_Relation],[Issue],[Thesis],[Thesis_Author],[Thesis_Author_Relation],[Issue_Groups],[Periodical],[Periodical_Type] where [Issue_Groups].Zid = [Issue].Zid and [Issue_Thesis_Relation].Ktid =[Issue].Ktid and [Issue_Thesis_Relation].Lwid = [Thesis].Lwid and [Periodical].lid = [Periodical_Type].lid and [Thesis].qkid = [Periodical].qkid and [Thesis_Author_Relation].Lwid = [Thesis].Lwid and [Thesis_Author_Relation].Zzid = [Thesis_Author].Zzid"; DataSet myds = new DataSet(); myds = SQLHelper.GetDataSet(sqlstr); int Sqlcount = myds.Tables[0].Rows.Count;//总记录数 this.AspNetPager1.RecordCount = Sqlcount; AspNetPager1.AlwaysShow = true; int pageSize = this.AspNetPager1.PageSize = 10;//每页记录数 int startIndex = (this.AspNetPager1.CurrentPageIndex - 1) * pageSize + 1;//每页最开始索引 int startindex1 = startIndex + 10; string sqlstr1 = "select * from(select ROW_NUMBER() over(order by [Thesis].CreateTime desc) row,[Thesis].Lwid,[Issue].Kttm,[Thesis].LNd,[Thesis].Sci,[Thesis].lwtitle,[Periodical_Type].qklx,[Periodical].Qkmc,[Thesis_Author].Zzxm,[Issue_Groups].Zbmc from [Issue_Thesis_Relation],[Issue],[Thesis],[Thesis_Author],[Thesis_Author_Relation],[Issue_Groups],[Periodical],[Periodical_Type] where [Issue_Groups].Zid = [Issue].Zid and [Issue_Thesis_Relation].Ktid =[Issue].Ktid and [Issue_Thesis_Relation].Lwid = [Thesis].Lwid and [Periodical].lid = [Periodical_Type].lid and [Thesis].qkid = [Periodical].qkid and [Thesis_Author_Relation].Lwid = [Thesis].Lwid and [Thesis_Author_Relation].Zzid = [Thesis_Author].Zzid)a where a.row>='" + startIndex + "'and a.row<'" + startindex1 + "'"; DataSet myds1 = new DataSet(); myds1 = SQLHelper.GetDataSet(sqlstr1); if (myds1.Tables[0].Rows.Count != 0) { GridView1.DataSource = myds1; GridView1.DataBind(); } else { DataTable mytable = new DataTable(); mytable.Columns.Add("CssClass", System.Type.GetType("System.String")); mytable.Columns.Add("lwid"); mytable.Columns.Add("lwtitle"); mytable.Columns.Add("qklx"); mytable.Columns.Add("Qkmc"); mytable.Columns.Add("Zzxm"); mytable.Columns.Add("Sci"); mytable.Columns.Add("Zbmc"); mytable.Columns.Add("LNd"); mytable.Columns.Add("kttm"); DataRow myrow = mytable.NewRow(); mytable.Rows.Add(myrow); DataSet myset = new DataSet(); myset.Tables.Add(mytable); GridView1.DataSource = myset; GridView1.DataBind(); GridView1.Rows[0].Visible = false; } }