国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

DataGrid常見(jiàn)解決方案(五)--- 在DataGrid產(chǎn)生空行紀(jì)錄

2024-07-21 02:23:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
在應(yīng)用程序中,我需要在一個(gè)datagrid中每顯示10條紀(jì)錄后便添加一個(gè)空行,我們可以簡(jiǎn)單的修改datatable,并且在datagrid中的itemdatabound 事件書寫一些代碼來(lái)實(shí)現(xiàn),下面的文章將闡述如何做到這一點(diǎn)。

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條紀(jì)錄添加一個(gè)空行看起來(lái)使用一個(gè)簡(jiǎn)單的循環(huán)就可以做到
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 = "&nbsp;"
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.
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 中卫市| 禹城市| 宁陕县| 黄石市| 民乐县| 环江| 海林市| 鄄城县| 浦县| 北宁市| 囊谦县| 茶陵县| 东乡| 兴化市| 兴安县| 阳春市| 长寿区| 永德县| 绵竹市| 田东县| 南木林县| 阳山县| 黔南| 兴义市| 乐亭县| 新龙县| 宝坻区| 普定县| 那曲县| 青州市| 三台县| 蕲春县| 綦江县| 永仁县| 凭祥市| 昂仁县| 虎林市| 屏东县| 龙门县| 石柱| 平乡县|