Empty row record generated in the DataGrid

Source: Internet
Author: User

Empty row record generated in the DataGrid

Hbzxf (Hao)
Www.cnblogs.com/hbzxf

?

In the application, I need to add an empty row after each 10 records are displayed in a DataGrid. We can simply modify the datatable andItemdataboundThe following article describes how to implement the event by writing code.

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 datagridsItemdataboundEvent. The rest of this article will describe how it is done.


Add blank rows to datatable
Adding blank rows to the datatable

?

Adding an empty row for each 10 records seems to be done using a simple loop.
To add a blank row every 10 rows seems simple enough to do using a for loop with a counter. I knew that I cocould 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 inItemdataboundEvent, the fact that it is a negative value can be used to note that this shoshould 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 cocould not be blank, and I knew that the values from the database wocould 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
???????? 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


Reflect empty rows to the 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 DataGridItemdataboundEvent 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 thatListitemIs bound to can then be accessed to check the value in the first column to see if the value it contains denotes that it shoshould 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 setsBackcolorTo 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 (E. Item. dataitem, datarowview). Row. Item (0), integer )???????????? 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.