张锋的博客

http://www.sjzrc.cn(石家庄人才)

张锋ID:hbzxf
139513次访问,排名510好友0人,关注者0
hbzxf的文章
原创 54 篇
翻译 0 篇
转载 3 篇
评论 157 篇
张锋的公告


朋友自远方来,不亦乐乎


个人简介
网名:hbzxf(阿好)
QQ:6055538
QQ群:5461477
MSN:hbzf@hotmail.com

来自:石家庄  | 管理 | 留言

最近评论
ljm2008:有没有第二节拿出来分享一下啊
liquanzhi11:想与本站连接一下
[url="http://www.hebei01.com]河北生活信息网[/url]
chenyanpeng2003:服了你了 点一个页面弹出一个广告 你这样做广告只有让人厌烦!!
jxq19881013:很感谢你,你的相当的实用.
Backli:http://blog.csdn.net/Backli/

友情连接吧

你的我已经添加了
文章分类
收藏
相册
BLOG友情连接
Eric's Blog[水王]
不平凡小高
千山一鸟[帅哥]
最爱白菜[蔬菜]
木子MM[美女]
牛人三哥[张老三]
石家庄人才
蛙蛙池塘[动物]
阿好.Net[野人]
阿赖.COM[赖皮]
最爱网址
CODEPROJECT
DotNetJunkies
MSDN中文
MSDN英文
Weblogs @ ASP.NET
博客堂
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

原创 在DataGrid产生空行纪录收藏

新一篇: ASP.NET如何解决页面之间传输中文乱码的问题

在DataGrid产生空行纪录

hbzxf(阿好)
www.cnblogs.com/hbzxf

?

在应用程序中,我需要在一个DataGrid中每显示10条纪录后便添加一个空行,我们可以简单的修改DataTable,并且在DataGrid中的ItemDataBound 事件书写一些代码来实现,下面的文章将阐述如何做到这一点。

In one application I had the requirement to add a blank row after every 10 rows in a DataGrid rather than use paging as shown in FIGURE 1. There is no in-built way to do this with the DataGrid, but it can be easily done by modifying the DataTable that the DataGrid is bound to and by writing some code in the DataGrids
ItemDataBound event. The rest of this article will describe how it is done.

添加空行到DataTable
Adding Blank Rows to the DataTable

?

每10条纪录添加一个空行看起来使用一个简单的循环就可以做到
To add a blank row every 10 rows seems simple enough to do using a for loop with a counter. I knew that I could not use a for each loop due to me adding new items to the collection within the loop. As it turns out, you cannot use a for loop with a counter as the upper bound of the loop is not re-evaluated on every iteration! This means that the only way to loop through the item collection is to use a while loop with a counter.

To keep track of when to add a blank row, another counter is used that is decremented. When this counter reaches 1, a new row is added to the DataTable and is then reset. At the same time, the upper bound is incremented to note the addition of the new row.

The code below shows a function that can be used to add blank rows to the first DataTable in any DataSet. As you can see, the row is not actually blank. The first column in the row is given the negative of the counter. When the DataRow is inspected in the ItemDataBound event, the fact that it is a negative value can be used to note that this should be displayed as a blank row in the DataGrid. In this case, the first column in the DataSet that was used was a Primary Key and could not be blank, and I knew that the values from the database would all be positive. When implementing this yourself you can use whatever identifier is suitable for your scenario.

Private Function addBlankLines(ByVal ds As DataSet) As DataSet

????Dim dr, drBlank As DataRow
????Dim count, repeatCount, upperBound As Integer

????repeatCount = 10 'used to keep track of when to add a 'blank' row
????upperBound = ds.Tables(0).Rows.Count

????While count <= upperBound

????????If repeatCount = 1 Then

????????????drBlank = ds.Tables(0).NewRow
????????????drBlank(0) = -count
????????????ds.Tables(0).Rows.InsertAt(drBlank, count + 1)
????????????count += 1
????????????upperBound += 1
????????????repeatCount = 10

????????Else

????????????repeatCount -= 1

????????End If

????????count += 1

????End While

????Return ds

End Function


把空行反映到DataGrid中
Rendering the Blank Rows to the DataGrid

After adding the blank rows to the DataTable, the next step is to render the blank rows in the DataGrid. To do this, a few lines in the DataGrid ItemDataBound event are required. The first thing to do is check to see if the item is an Item or Alternating item as this event fires for both the header and the footer of the DataGrid. The underlying DataRow that the ListItem is bound to can then be accessed to check the value in the first column to see if the value it contains denotes that it should be rendered as a blank row. The code below sets the text of the first cell to contain so as to make the blank row visible, and sets the BackColor to White (the rest of the rows are presented in a different color). This code can be easily adapted to allow you to render whatever format of blank row that you want.

Private Sub dgResults_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgResults.ItemDataBound
????If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

????????If CType(CType(e.Item.DataItem, DataRowView).Row.Item(0), Integer) < 0 Then
????????????e.Item.Cells(0).Text = " "
????????????e.Item.BackColor = System.Drawing.Color.White
????????End If

????End If

End Sub

Alternative Use

One alternative use for the idea and code presented here is to create a running total row. As the DataTable is looped through, a running total can be kept and that value inserted into the correct column in the DataTable. Instead of rendering a blank row in the DataGrid, it can be rendered in another format.

发表于 @ 2004年05月31日 16:47:00|评论(loading...)|编辑

旧一篇: 给DataGrid单元行添加双击事件

评论

#比儿 发表于2004-07-04 22:25:00  IP: 221.11.20.*

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 张锋