2015年7月14日16:50:06  Gridview 默认展示数据时,若数据为空,则表格不显示,显示不美观。

针对此问题进行扩展:

using System.Web.UI.WebControls;

public static class GridViewExtension
{
public static void BindEmptyData(this GridView g)
{
if (g.Rows.Count == )
{
//表头的设置
GridViewRow row = new GridViewRow(-, -, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
foreach (DataControlField field in g.Columns)
{
TableCell cell = new TableCell();
cell.Text = field.HeaderText;
cell.Width = field.HeaderStyle.Width;
cell.Height = field.HeaderStyle.Height;
cell.ForeColor = field.HeaderStyle.ForeColor;
cell.Font.Size = field.HeaderStyle.Font.Size;
cell.Font.Bold = field.HeaderStyle.Font.Bold;
cell.Font.Name = field.HeaderStyle.Font.Name;
cell.Font.Strikeout = field.HeaderStyle.Font.Strikeout;
cell.Font.Underline = field.HeaderStyle.Font.Underline;
cell.BackColor = field.HeaderStyle.BackColor;
cell.VerticalAlign = field.HeaderStyle.VerticalAlign;
cell.HorizontalAlign = field.HeaderStyle.HorizontalAlign;
cell.CssClass = field.HeaderStyle.CssClass;
cell.BorderColor = field.HeaderStyle.BorderColor;
cell.BorderStyle = field.HeaderStyle.BorderStyle;
cell.BorderWidth = field.HeaderStyle.BorderWidth;
row.Cells.Add(cell);
}
TableItemStyle headStyle = g.HeaderStyle;
TableItemStyle emptyStyle = g.EmptyDataRowStyle;
emptyStyle.Width = headStyle.Width;
emptyStyle.Height = headStyle.Height;
emptyStyle.ForeColor = headStyle.ForeColor;
emptyStyle.Font.Size = headStyle.Font.Size;
emptyStyle.Font.Bold = headStyle.Font.Bold;
emptyStyle.Font.Name = headStyle.Font.Name;
emptyStyle.Font.Strikeout = headStyle.Font.Strikeout;
emptyStyle.Font.Underline = headStyle.Font.Underline;
emptyStyle.BackColor = headStyle.BackColor;
emptyStyle.VerticalAlign = headStyle.VerticalAlign;
emptyStyle.HorizontalAlign = headStyle.HorizontalAlign;
emptyStyle.CssClass = headStyle.CssClass;
emptyStyle.BorderColor = headStyle.BorderColor;
emptyStyle.BorderStyle = headStyle.BorderStyle;
emptyStyle.BorderWidth = headStyle.BorderWidth;
//空白行的设置
GridViewRow row1 = new GridViewRow(, -, DataControlRowType.EmptyDataRow, DataControlRowState.Normal); TableCell cell1 = new TableCell(); cell1.Text = "没有任何数据可以显示";
cell1.ForeColor = System.Drawing.Color.Purple;
cell1.BackColor = System.Drawing.Color.White;
row1.Cells.Add(cell1);
cell1.ColumnSpan = g.Columns.Count; if (g.Controls.Count == )
{
g.Page.Response.Write("<script language='javascript'>alert('请先调用GridView的BindData()方法后 调用此扩展方法!');</script>");
}
else
{
g.Controls[].Controls.Clear();
g.Controls[].Controls.AddAt(, row);
g.Controls[].Controls.AddAt(, row1);
}
}
}
}

代码调用方式如下:

    protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
} protected void BindData()
{
this.grid_PlantVarietyRight.DataBind();
this.grid_PlantVarietyRight.BindEmptyData(); }

显示效果如下图

本扩展主要参考  http://www.cnblogs.com/muer/archive/2010/03/26/1697178.html  稍作扩展,感谢这位哥。

扩展:gridview 空数据时显示表头的更多相关文章

  1. 实现easyui datagrid在没有数据时显示相关提示内容

    本示例实现easyui datagrid加载/查询数据时,如果没有相关记录,则在datagrid中显示没有相关记录的提示信息,效果如下图所示 本实例要实现如下图所示的效果: 本示例easyui版本为1 ...

  2. easy ui datagrid在没有数据时显示相关提示内容

    $(function () { $('#dg').datagrid({ fitColumns: true, url: 'product.json', pagination: true, pageSiz ...

  3. easyui datagrid 没数据时显示滚动条的解决方法

    今天解决了一个bug,因为datagrid有多列,可是当没有数据的时候,后面的列无法通过滚动条拉动来显示,比较麻烦,而需求要求没有数据也要拉动滚动条查看后面有什么列,一开始在网上找了一些资料,发现都不 ...

  4. ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)

    介绍ASP.NET AJAX可以使你的web应用程序具有更丰富的功能和更多的用户响应. 本文中,我将演示如何通过ASP.NET AJAX的帮助,给像GridView这样的数据绑定控件的数据行增加pop ...

  5. UWP GridView切换数据时界面闪动

    在选择数据时,比如1-10集,和11-20集切换时,GridView需要切换对应的数据,但是会发生界面闪动. 这是默认的Item Transition导致的. 可以去掉默认的转换效果. <Gri ...

  6. MVC bootstrap-table显示数据时显示No matching records found

    问题:bootstrap-table加载数据不显示 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layou ...

  7. laravel post提交数据时显示异常

    post提交数据时候显示如下: The page has expired due to inactivity. Please refresh and try again 这是由于在laravel框架中 ...

  8. hive命令查询数据不显示表头解决办法

    在hive命令行中查询数据如下: 表头未显示出来 解决办法: 修改hive安装包conf/hive-site.xml配置文件: <property> <name>hive.cl ...

  9. 关于EasyUI 1.5版Datagrid组件在空数据时无法显示"空记录"提示的BUG解决方法

    问题:jQuery easyUI中Datagrid,在表格数据加载无数据的时候,如何显示"无记录"的提示语? 解决jQuery EasyUI 1.5.1版本的Datagrid,在处 ...

随机推荐

  1. 关于Excel Networkdays方法的实现

    最近一个程序要求excel输出的日期差为Networkdays. 在网上找了下,没有找到很好的具体实现方法. 要说明的是,微软的Microsoft.Office.Interop.Excel已经实现的N ...

  2. httpclient模拟浏览器訪问站点

    HttpClient 是 Apache Jakarta Common 下的子项目.能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的client编程工具包.而且它支持 HTTP 协议最新的版 ...

  3. 眼下最好的JSP分页技术

     2005-08-24   来源:CSDN  作者:wanchao2001 前言     在使用数据库的过程中,不可避免的须要使用到分页的功能,但是JDBC的规范对此却没有非常好的解决.对于这个需求非 ...

  4. 一步一步写算法(之hash表)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] hash表,有时候也被称为散列表.个人觉得,hash表是介于链表和二叉树之间的一种中间结构.链 ...

  5. android115 自定义控件

    布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  6. C语言---注释

    1.单行注释 //注释内容 2.多行注释 /*注释内容*/ 3.#if 0 注释内容 #endif 说明: 1.单行注释//不通用,有些编译器不支持 2.多行注释不能嵌套 3.#if 0 #endif ...

  7. yii下使用oracle中文都变成问号乱码的解决方法

    在配置文件中,一般会用以下配置: 'db_oracle'=>array( 'class' => 'CDbConnection', 'connectionString'=>'oci:d ...

  8. Centos 6.4 安装elasticsearch+kibana

    elasticsearch和kibanna的链接地址:https://www.elastic.co/downloads,我的环境里用的包为kibana-4.1.1-linux-x64.tar.gz和e ...

  9. 面试相关的技术问题--WEB基础

    1. servlet生命周期.各个方法 和工作原理servlet的生命周期包括三个阶段,分别是:初始化阶段:调用init()方法(整个生命周期内只被调用一次)响应客户端请求阶段:service()终止 ...

  10. 【HTML XHTML CSS基础教程(第6版)】笔记之HTML XHTML笔记(1~6章)

      第1章 网页的构造块   1.(X)HTML有三种主要的标记类型:元素,属性,值.   2.浏览器主要通过查看文件的扩展名(.htm或.html)来得知应该按照网页的方式读取文本文件.   3.H ...