GridView 的EmptyDataText 及 EmptyDataTemplate

当GridView绑定的数据源为Null或查询绑定的DataSet等为空时
GridView显示没有数据的提示

有如下几种方式

一是用GridView的EmptyDataText
-------------------------------
<asp:GridView ID="gv_Info" runat="server" CssClass="GridView"
   EditRowStyle-HorizontalAlign="center" AutoGenerateColumns="True"
   AllowPaging="True" PageSize="4"  Width="100%" RowStyle-HorizontalAlign="center"
   AllowSorting="True" OnSorted="gv_Info_Sorted" OnSorting="gv_Info_Sorting"
   EmptyDataRowStyle-CssClass="GridViewNoData"
   EmptyDataText="暂没有数据"
   EmptyDataRowStyle-HorizontalAlign="center"
   EmptyDataRowStyle-Font-Bold="true"
>  

后台可用如下方式进行修改其显示

this.gv_Info.EmptyDataText = "No No No Data";

           
二是用GridView的EmptyDataTemplate
-------------------------------
<EmptyDataTemplate>
   <table width="100%" cellpadding="0" cellspacing="0" class="">
         <tr align="center">
              <td style="height: 40px;" runat="server"  id="td_EmptyData">
                     <b>请点选[统计查看]按钮 或者 统计暂没有数据</b></td>
              </tr>
    </table>
    <asp:Label runat="server" ID="lbl_EmptyData" Text="No Data"></asp:Label>
</EmptyDataTemplate>

后台可用如下方式进行修改其显示

Table gvTable = ((Table)this.gv_Info.Controls[0]);
Label tmpLabel = (Label)(gvTable.Rows[0].FindControl("lbl_EmptyData"));
tmpLabel.Text = "No No No Data";

三是当然也可以在GridView没有显示时 显示预先设定好的Table

附:
上述两种方式一起使用时 显示时将以第二种方式为主

再有 清空GridView显示
//this.gv_Info.EmptyDataText = "No Data";
this.gv_Info.DataSource = null;
this.gv_Info.DataBind();

 

 

 

 

 

 

 

 

 

 

 

 

增加空记录提示
我们在数据绑定后增加一些代码对GridView进行一下处理,让显示结果更友好。在this.GridViewEmptyDataTest.DataBind();后面增加代码如下所示:

  int columnCount = dt.Columns.Count;

  GridViewEmptyDataTest.Rows[0].Cells.Clear();

  GridViewEmptyDataTest.Rows[0].Cells.Add(new TableCell());

  GridViewEmptyDataTest.Rows[0].Cells[0].ColumnSpan = columnCount;

  GridViewEmptyDataTest.Rows[0].Cells[0].Text = "没有记录";

  GridViewEmptyDataTest.Rows[0].Cells[0].Style.Add("text-align", "center");

改良后的显示结果如下图所示:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值