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>
效果