DataGrid常见解决方案(五)--- 在DataGrid产生空行纪录

datagrid|解决

在应用程序中,我需要在一个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.

时间: 2024-08-31 09:37:48

DataGrid常见解决方案(五)--- 在DataGrid产生空行纪录的相关文章

DataGrid常见解决方案:在分页状态下删除纪录的问题

datagrid|分页|解决|问题 在使用DataGrid分页的时候,正常情况下,绑定数据库列表纪录时会自动产生分页的效果,然而我发觉在删除纪录的时候总会发生"无效的 CurrentPageIndex 值.它必须大于等于 0 且小于 PageCount."的异常,其实解决这个问题很简单,我们要做的就是在DataGrid1_DeleteCommand事件中判断CurrentPageIndex的值,并根据不同的结果来绑定DataGrid. //检索数据库的函数 public DataSet

DataGrid常见解决方案(三)--在DataGrid中选择,确认,删除多行复选框列表

datagrid|复选框|解决 在DataGrid中选择,确认,删除多行复选框列表 Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo) Introduction Although I don't have either a Hotmail or Yahoo email account, I do have friends that do, and I ha

DataGrid常见关注问题解决方案

datagrid|解决|问题 IntroductionThe DataGrid Web server control is a powerful tool for displaying information from a data source. It is easy to use; you can display editable data in a professional-looking grid by setting only a few properties. At the same

ASP.NET中Datagrid常见错误

asp.net|datagrid|错误 摘要:学习如何避免在使用 ASP.NET Datagrid 控件进行开发时可能发生的一些常见错误. Datagrid 控件是 Microsoft? ASP.NET 中功能最强.用途最广的 Web 控件之一,这一点已经得到了 ASP.NET 权威人士的认同.虽然 Datagrid 控件易于使用,但同样易于给使用者带来麻烦.以下是许多人所犯的一些错误,这些人包括从初学者到富有经验的 .NET 专家.您可以看到许多苦闷的使用者在 ASP.NET 新闻组和论坛就这

求数据库显示在datagrid例子并打印出datagrid内容.急急急!!!

问题描述 是想把这句"selectsum(a),sum(b),sum(C)fromtables1whereworkdate='20071108'andbrnoin(selectbrnofromtables2wherembrnoin('4000','4010'))andcurrtype='001'andsubnolike'i%'"中的sum(a),sum(b),sum(c)所求得的值显示在datagrid里面并可以打印出来简单的数据库显示在datagrid里面和把datagrid的打印~

盘点监视器最常见的五种故障

 监视器常见故障一: 视频传输中,最常见的故障现象表现在监视器的画面上出现一条黑杠或白杠,并且或向上或向下慢慢滚动.因此,在分析这类故障现象时,要分清产生故障的两种不同原因.要分清是电源的问题还是地环路的问题,一种简易的方法是,在控制主机上,就近只接入一台电源没有问题的摄像机输出信号,如果在监视器上没有出现上述的干扰现象,则说明控制主机无问题.接下来可用一台便携式监视器就近接在前端摄像机的视频输出端,并逐个检查每台摄像机.如有,则进行处理.如无,则干扰是由地环路等其它原因造成的. 监视器常见故障

浅谈网站建设中常见的五种误区

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 大家都知道网站建设相对网站推广来说是比较简单的,因为现在利用CMS系统能够很容易的架构起来一个功能非常足的网站,但是如果把这个网站推广出去却变得非常的困难,实际上在网站建设的时候只要避免下面的五种误区就能够增加推广的效果,甚至能够起到事半功倍的作用,下面我们就来介绍网站建设中常见的五种误区! 一:友情链接的误区 友情链接对于一个网站来说是非常

常见的五类排序算法图解和实现(多关键字排序:基数排序以及各个排序算法的总结)

基数排序思想 完全不同于以前的排序算法,可以说,基数排序也叫做多关键字排序,基数排序是一种借助"多关键字排序"的思想来实现"单关键字排序"的内部排序算法. 两种方式: 1.最高位优先,先按照最高位排成若干子序列,再对子序列按照次高位排序 2.最低位优先:不必分子序列,每次排序全体元素都参与,不比较,而是通过分配+收集的方式. 多关键字排序 例:将下表所示的学生成绩单按数学成绩的等级由高到低排序,数学成绩相同的学生再按英语成绩的高低等级排序.        第一个关键

网页设计中常见的五种交互设计错误

  交互设计从来都不是简单的事情.可靠的交互通常需要借助对用户行为的深入分析,然后有针对性的精心策划.可是随着新的技术和新的交互模式的不断涌现,事情就变得不是那么容易了. 其实大家遭遇的困境都很相似,用户已经很难被单纯炫酷的图片.流畅的悬停效果和出人意料的动效所打动,真正能让他们露出微笑的是持久.令人愉悦的交互设计和用户体验.如果你对于日常的设计中的常见的误区有所了解,自然会有意识地绕过这些坑. 为了更好的设计,我们为你简单总结了一下最常见的5种交互设计的误区. 1.过度的创意 网页设计师绝对不