首页 > 代码库 > GridView中如何在无数据时显示空行和表头

GridView中如何在无数据时显示空行和表头

#region ---- 数据绑定 -----

    private void bd(int pageindex)
    {
        int rowcount = 0;
        int pagesize = 0;
        int pagecount = 0;
        decimal cyfzhj = 0;
        string Slqstring = Base_Query_Sql;
       
        mta.DT(Slqstring);
        DataTable rs = mta.DT(Slqstring);
        this.GridView1.DataSource = rs;
        rowcount = rs.Rows.Count;

        if (rowcount == 0)
        {
            rs.Columns.Clear();
            rs.Columns.Add("nid");
            rs.Columns.Add("title");
            rs.Columns.Add("nTime");
            rs.Columns.Add("una");
            rs.Columns.Add("author");
            rs.Columns.Add("yhm");
            rs.Columns.Add("shbz");
            rs.Columns.Add("shTime");
            rs.Columns.Add("ssbm");
            rs.Columns.Add("csTime");

            rs.Columns.Add("cyTime");
            rs.Columns.Add("shbzmc");
            rs.Columns.Add("cyfz");
 


            DataRow dr = rs.NewRow();
            dr["nid"] = " ";
            dr["title"] = " ";
            dr["nTime"] = " ";
            dr["una"] = " ";
            dr["author"] = " ";
            dr["yhm"] = " ";
            dr["shbz"] = " ";
            dr["shTime"] = " ";
            dr["ssbm"] = " ";
            dr["csTime"] = " ";
            dr["cyTime"] = " ";
            dr["shbzmc"] = " ";
            dr["cyfz"] = " ";

            rs.Rows.Add(dr);
            this.GridView1.DataBind();
            return;
        }

        pagesize = this.GridView1.PageSize;
        pagecount = rowcount / pagesize;
        if (pagecount * pagesize < rowcount)
        {
            pagecount = pagecount + 1;
        }
        if (((pageindex > pagecount - 1) && (pagecount != 0)) || (pageindex < 0))//如果给定的pageindex超出范围
        {
            pageindex = pagecount - 1;
        }
        GridView1.PageIndex = pageindex;
        GridView1.EditIndex = -1;
        GridView1.DataKeyNames = new string[] { "nid" };
        this.GridView1.DataBind();
    }
    #endregion
绑定之前判断datatable是否为空 
如果为空 手工加入一行数据