GridView无数据时显示表头
在.NET2.0中GridView的功能已经十分的强大,可是,在和数据库中的数据关联后,在显示方面也有不够完美的地方:那就是如果没有数据时,连表头信息也不能显示。 如果要显示表头信息该怎么办?这让我思量了好久,最终找到了解决的方案。 首先,在选中GridView控件点击右键,选择“编辑模板”->“EmptyDataTemplate”,在“EmptyDataTemplate”项中,编辑一个表,把表头信息(即标题)写入表中即可。每列的表头宽度定义为需要显示的宽度(在EmptyDataTemplate中写入表头信息如下)。
<table> <tr style=" color:Black; background-color:SkyBlue; font-weight:bold;" > <th scope="col" style="width:10px;"> </th> <th scope="col" style="width:190px;">编号</th> <th scope="col" style="width:194px;">名称</th> <th scope="col" style="width:190px;">日期</th> <th scope="col" style="width:100px;">周期(周)</th> <th scope="col" style="width:110px;">详细</th> </tr> </table>
然后,在后台代码中加上没有数据时的显示表头的方法(方法如下),当然在加入该方法前需要判断是否有数据。
private void bindULData()
{
SqlConnection sqlCon = new SqlConnection("server=.;database=testUL;uid=sa;pwd=sa");
SqlCommand sqlCom = new SqlCommand("select * from UserInfo where FID = 10", sqlCon);
SqlDataAdapter sda = new SqlDataAdapter(sqlCom);
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
else
{
DataTable dt = new DataTable();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}


浙公网安备 33010602011771号