惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
W
WeLiveSecurity
IT之家
IT之家
A
About on SuperTechFans
B
Blog
L
LangChain Blog
H
Help Net Security
Engineering at Meta
Engineering at Meta
Recent Announcements
Recent Announcements
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
GbyAI
GbyAI
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Cloudbric
Cloudbric
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
S
Security Affairs
The Last Watchdog
The Last Watchdog
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
博客园 - 聂微东
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
V
Visual Studio Blog
H
Hacker News: Front Page
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Jina AI
Jina AI
N
News | PayPal Newsroom
S
Schneier on Security

博客园 - zsi

在线程中调用SaveFileDialog DSOFramer 之一:在 64 位系统注册 DSOFramer GridView 始终显示 Pager 分页行的一种方法 Chrome: Google加入浏览器大战之兼容性 调用unrar.dll时SEHException外部组件异常的处理 ASP.NET 2.0无法打开到 SQL Server 的连接 扯扯OpenFileDialog和.NET的缺省目录 给ASP.NET程序换换地儿 对象序列化:经验小结 对象序列化:使用XmlSerializer走完最后一步 对象序列化:使用System.Xml.Serialization命名空间 在.NET中实现对象序列化 了解HTTP协议一些有用资料 Yahoo!十岁! 在VB.NET中处理构造函数时值得注意的两个陈述 微软新发布的共享设计模式的WIKI 还不快进入Design Pattern的世界? 另人费解的IsNot关键字 也说金山词霸2005内存泄露的问题
GridView 绑定数据不满一页时填充空行的方法
zsi · 2008-10-10 · via 博客园 - zsi

ASP.NET学习笔记系列:

ASP.NET 2.0 提供了非常强大的绑定能力,而 ASP.NET 2.0 新增的 GridView 控件更是充分体现和发挥了数据绑定强大的功能。GridView 的数据绑定和页面显示可以通过简单的代码实现,大大提高了开发效率。

不过有个地方让人不爽,就是在使用分页时,如果绑定的数据不满一页,那么 GridView 仅显示绑定的数据。例如,将 GridView 的 PageSize 设置为 10,每页显示 10 条数据,如果当前查询仅返回了 6 条数据,那么 GridView 就显示这 6 条数据,如果当前查询返回了多于 10 条数据,那么 GridView 则显示 10 条数据,这样当执行不同的查询时,GridView 时而显示 10 条数据,时而显示 1、2、3、……,9 条数据,GridView 的显示区域随着绑定的数据条数的变化而变化,给人很糟糕的交互体验。

怎么办?如果能够不管绑定的数据条数是多是少,都能始终显示相同的行数就好了,也就是说,当 GridView 绑定的数据条数不满一页时,通过某种方法在 GridView 中添加空行,使 GridView 能够始终显示 PageSize 设置的行数。

 那么怎样添加空白行呢?还是利用 GridView 的 DataBound 事件,在绑定结束后,通过判断 GridView 的 Rows 属性将行数补充到和 PageSize 设置的大小一样,具体代码和效果如下:

    public void gridView_DataBound(object sender, EventArgs e)
    {
        
if (gridView.Rows.Count != 0 && gridView.Rows.Count != gridView.PageSize)
        {
            Control table 
= gridView.Controls[0];
            
if (table != null)
            {
                
for (int i = 0; i < gridView.PageSize - gridView.Rows.Count; i++)
                {
                    
int rowIndex = gridView.Rows.Count + i + 1;
                    GridViewRow row 
= new GridViewRow(rowIndex, -1, DataControlRowType.Separator, DataControlRowState.Normal);
                    row.CssClass 
= (rowIndex % 2 == 0? "alternate" : "item";
                    
for (int j = 0; j < gridView.Columns.Count; j++)
                    {
                        TableCell cell 
= new TableCell();
                        cell.Text 
= "&nbsp;";
 
                       row.Controls.Add(cell);
                    }
                    table.Controls.AddAt(rowIndex, row);
                }
            }
        }
    }

需要注意到是:

1. 添加的 GridViewRow 的 RowType 需要设置成 Seperator;

2. 设置空白行内地 TableCell 属性时,应注意 GridView 对应列的 Visible 属性是否设置为 false,和 GridView 的 AutoGenerateCollomn 是否设置为 true,如果是,那么应该对应的做一些处理。

最后的填充了空白行的 GridView 效果如图: