• GridView 无数据时,绑定提示


     1  private void BindData()
     2     {
     3         DataTable dt = DAO.RunSQLReturnDt(this.getsql());
     4         int dtcount = dt.Rows.Count;
     5         if (dtcount == 0)
     6         {
     7             dt.Rows.Add(dt.NewRow());
     8         } 
     9         DataView dv = dt.DefaultView;
    10         //对用于分页的类的引用
    11         PagedDataSource pds = new PagedDataSource();
    12         pds.DataSource = dv;//设置数据源(DataTable类型)
    13         pds.AllowPaging = true;
    14         //每页显示的行数
    15         pds.PageSize = 20;
    16         AspNetPager1.RecordCount = dv.Count;
    17         pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
    18         pds.PageSize = AspNetPager1.PageSize;
    19         GridView1.DataSource = pds;
    20         GridView1.DataBind();
    21         if (dtcount <= 0)
    22         {
    23             int colnumcount = dt.Columns.Count;
    24             GridView1.Rows[0].Cells.Clear();
    25             GridView1.Rows[0].Cells.Add(new TableCell());
    26             GridView1.Rows[0].Cells[0].ColumnSpan = colnumcount;
    27             GridView1.Rows[0].Cells[0].Text = "没有相关记录";
    28             GridView1.Rows[0].Cells[0].Style.Add("color", "red");
    29         }
    30 
    31 
    32     }

    页面正常显示

     1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="tablelist" GridLines="None" DataKeyNames="C_ID" OnRowDataBound="GridView1_RowDataBound">
     2                     <Columns>
     3                         <asp:BoundField DataField="C_AppNum" HeaderText="入库编号" SortExpression="C_AppNum">
     4                             <ItemStyle HorizontalAlign="Center" />
     5                         </asp:BoundField>
     6 
     7 
     8                         <asp:BoundField DataField="C_AppDate" HeaderText="申请时间" SortExpression="C_AppDate">
     9                             <ItemStyle HorizontalAlign="Center" />
    10                         </asp:BoundField>
    11                         <asp:TemplateField HeaderText="申请部门">
    12                             <ItemTemplate>
    13                           <%# XC.Depart.GetDepartName(Eval("C_AppDepartID").ToString()) %>
    14                             </ItemTemplate>
    15                             <ItemStyle HorizontalAlign="Center" />
    16                         </asp:TemplateField>
    17                         <asp:TemplateField HeaderText="申请人">
    18                             <ItemTemplate>
    19                                 <%# XC.User.GetUserName(Eval("C_AppUserID").ToString())%>
    20                             </ItemTemplate>
    21                             <ItemStyle HorizontalAlign="Center" />
    22                         </asp:TemplateField>
    23                         <asp:BoundField DataField="C_Status" HeaderText="状态" SortExpression="C_Status">
    24                             <ItemStyle HorizontalAlign="Center" />
    25                         </asp:BoundField>
    26                         <asp:BoundField DataField="C_AuditState" HeaderText="审核状态" SortExpression="C_AuditState">
    27                             <ItemStyle HorizontalAlign="Center" />
    28                         </asp:BoundField>
    29                        
    30                     </Columns>
    31                     <HeaderStyle HorizontalAlign="center" />
    32                 </asp:GridView>

    效果

  • 相关阅读:
    Tomcat中实现IP访问限制
    webservice ssl双向认证配置
    如何更专业的使用Chrome开发者工具
    C++中常量成员函数的含义
    写时拷贝COW(copy-on-write)
    char* 、const char*和string之间的转换
    C++模板特化与偏特化
    lamda表达式和尾置返回类型
    编译期多态和运行时多态
    静态绑定和动态绑定
  • 原文地址:https://www.cnblogs.com/YouthFingertips/p/5051056.html
Copyright © 2020-2023  润新知