当GridView没有查到对应的数据时的处理办法:
1、直接给GridView添加一个空数据的行。
DataTable dt=ds.Tables[0];
DataRow dr=dt.NewRow();
dt.Rows.InsertAt(dr,0);
GridView1.DataSource=dt;
GridView1.DataBind();
2、可以用GridView自带的模板中的方法(EmptyDataTemplate)
如:当GridView为空时给之插入一个类似的Table。
<EmptyDataTemplate>
<table width="100%">
<tr>
<td>
姓名</td>
<td>
年龄</td>
<td>
状态</td>
<td><asp:Label ID="lblTime" runat="server" Text="发货时间"></asp:Label></td>
<td>
操作</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
如果后台中要调用EmptyDataTemplate中的服务器控件的话可以用此方法:
例如调用后台中的Label控件lblTime
以下代码必须写在GridView中的RowDataBound事件中:
if(e.Row.RowType==DataControlRowType.EmptyDataRow)
{
Label lblCell=(Label)GridView1.Controls[0].Controls[0].FindControl("lblTime"); //第一层Control是Table,第二层Control是Row
lblCell.Text="下单时间";
}