扩展:gridview 空数据时显示表头
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 空数据时显示表头的更多相关文章
- 实现easyui datagrid在没有数据时显示相关提示内容
本示例实现easyui datagrid加载/查询数据时,如果没有相关记录,则在datagrid中显示没有相关记录的提示信息,效果如下图所示 本实例要实现如下图所示的效果: 本示例easyui版本为1 ...
- easy ui datagrid在没有数据时显示相关提示内容
$(function () { $('#dg').datagrid({ fitColumns: true, url: 'product.json', pagination: true, pageSiz ...
- easyui datagrid 没数据时显示滚动条的解决方法
今天解决了一个bug,因为datagrid有多列,可是当没有数据的时候,后面的列无法通过滚动条拉动来显示,比较麻烦,而需求要求没有数据也要拉动滚动条查看后面有什么列,一开始在网上找了一些资料,发现都不 ...
- ASP.NET 使用AJAX让GridView的数据行显示提示框(ToolTip)
介绍ASP.NET AJAX可以使你的web应用程序具有更丰富的功能和更多的用户响应. 本文中,我将演示如何通过ASP.NET AJAX的帮助,给像GridView这样的数据绑定控件的数据行增加pop ...
- UWP GridView切换数据时界面闪动
在选择数据时,比如1-10集,和11-20集切换时,GridView需要切换对应的数据,但是会发生界面闪动. 这是默认的Item Transition导致的. 可以去掉默认的转换效果. <Gri ...
- MVC bootstrap-table显示数据时显示No matching records found
问题:bootstrap-table加载数据不显示 @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layou ...
- laravel post提交数据时显示异常
post提交数据时候显示如下: The page has expired due to inactivity. Please refresh and try again 这是由于在laravel框架中 ...
- hive命令查询数据不显示表头解决办法
在hive命令行中查询数据如下: 表头未显示出来 解决办法: 修改hive安装包conf/hive-site.xml配置文件: <property> <name>hive.cl ...
- 关于EasyUI 1.5版Datagrid组件在空数据时无法显示"空记录"提示的BUG解决方法
问题:jQuery easyUI中Datagrid,在表格数据加载无数据的时候,如何显示"无记录"的提示语? 解决jQuery EasyUI 1.5.1版本的Datagrid,在处 ...
随机推荐
- LeetCode144:Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- 学习笔记之DB2 9 Fundamentals 730
Sequence中cache的影响,每新建一个连接,next value值增加increment * cache.如果加上order,则会按顺序生成值. increment cache ; Conne ...
- [C++]对象的销毁机制
销毁时会按照从后向前的顺序销毁,也就是说,越在后面定义的对象会越早销毁.其中的原因就是函数是在栈中保存的,因此,先定义的对象先压栈,所以在退栈时就会后销毁.而如果参数有多个的话,大多数编译器是从右开始 ...
- PAT 1009
1009. Product of Polynomials (25) This time, you are supposed to find A*B where A and B are two poly ...
- C# 之 OpenFileDialog的使用
一.打开文件对话框(OpenFileDialog) 1. OpenFileDialog控件有以下基本属性 [1]InitialDirectory:对话框的初始目录 [2]Filter:要在对话框中显示 ...
- Linux Shell产生16进制随机数
n为字符长度 openssl rand -hex n
- WPF 中的绑定方式
1.元素间的绑定 xaml方式 <Slider Name="slider1" Value="20"/> <TextBlock T ...
- 【原】Spring与MongoDB集成:配置
MongoDB的API提供了DBObject接口来实现BSONObject的操作方法,BasicDBObject是具体实现.但是并没有提供DBObject与BeanObject的转换.在还没有了解到与 ...
- 【高级JEE技术】JMX
JMX即Java Manager Extentin(java 管理扩展)一种动态改变javabean属性值的技术,具体应用场景可以有很多.比如使用JMX作为线上应用的开关,在做一些新老系统改造的时候 ...
- ajax调用服务的基本格式
<个人积累,转载请注明出处> 格式如下: $.ajax({ type: "post", //访问WebService使用Post方式请求 url: "http ...