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. android自动填充短信验证码

    自动拦截短信实际上就是在系统注册一个BroadcastReceiver,然后通过设置拦截短信的: filter.addAction("android.provider.Telephony.S ...

  2. [MODx] 10. Using Babel for Muti-languages support

    1. Go to 'Extras' -> download and install 'Babel'. 2. Set up '.htaccess' file, currently, we set ...

  3. android120 zhihuibeijing 开机页面

    软件启动旋转动画: 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  4. C++中的头文件和源文件

    一.C++编译模式 通常,在一个C++程序中,只包含两类文件——.cpp文件和.h文件.其中,.cpp文件被称作C++源文件,里面放的都是C++的源代码:而.h文件则被称作C++头文件,里面放的也是C ...

  5. careercup-中等难度 17.3

    17.3 写一个算法计算n的阶乘末尾0的个数? 解答: 首先,算出n的阶乘的结果再去计算末尾有多少个0这种方法是不可取的, 因为n的阶乘是一个非常大的数,分分种就会溢出.我们应当去分析, 是什么使n的 ...

  6. node.js 针对不同的请求路径(url) 做出不同的响应

    边看这个边写的: http://wenku.baidu.com/link?url=C4yLe-TVH6060u_x4t34H3Ze8tjoL7HjJaKgH-TvHnEYl-T_gAMYwhmrCeM ...

  7. nopCommerce的源代码结构和架构

    下载地址 nopcommerce的源代码很容易拿到,它是开源的,所以你可以直接到网上下载.下载地址:http://www.nopcommerce.com/downloads.aspx 因为nop是国外 ...

  8. Android Studio配置Dagger2 以及butterknife

    一.配置butterknife 在build.gradle(Module)文件中的dependencies模块添加: dependencies { // add butterknife compile ...

  9. iOS 网络编程:JSON解析

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  10. GitHub 建立远程仓库

    终端所有信息: Last login: Fri Aug 14 08:58:01 on console wuxiaoyuan:~ lan$ ls -al ~/.ssh ls: /Users/lan/.s ...