DataGrid Common Solution (v)---generate blank line records in the DataGrid

Source: Internet
Author: User
Tags count integer
datagrid| solution in the application, I need to add a blank row after each 10 record in a DataGrid, we can simply modify the DataTable and write some code for the ItemDataBound event in the DataGrid. The following article will explain how to do this.

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


Add a blank line to a DataTable
Adding Blank Rows to the DataTable



Add a blank line per 10 record it looks like you can do it with a simple loop.
To add a blank row every the seems simple enough to does using a for loop with a counter. I knew that I could "not" a For Each loop due to me adding new items to the collection within the loop. As it turns out, your cannot use a for loop with a counter as the upper bound to the loop is not re-evaluated on every ITER ation! This means so the only way to loop through the item collection are to use a while loop with a counter.

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

The code below shows a function that can is used to add blank rows to the "the" "the" the "a" As you can, the row isn't actually blank. The ' the ' negative of the ' the ' given the ' counter. When the DataRow was inspected in the ItemDataBound event, the fact this it is a negative value can being used to note that th Is should being displayed as a blank row in the DataGrid. In this case, the the the "the" in "the DataSet" that is used was a Primary Key and could is blank, and I knew that Values from the database would all is positive. When implementing this yourself the can use whatever identifier are 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 = Ten ' used to keep track of the 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



To reflect a blank line in a DataGrid
Rendering the Blank Rows to the DataGrid

After adding the "blank" to the "DataTable", the next step was to render the blank rows in the DataGrid. To does this, a few lines in the DataGrid ItemDataBound event are required. The "the" to "is" check to "to" thing "to" "if" is "is" an "item as this event fires for both the header a nd the footer of the DataGrid. The underlying DataRow that the ListItem are bound to can then being accessed to check the The value it contains denotes that it should to be rendered as a blank row. The code below sets the "the" the "the" the "the" the "the" the "the" to "to" to "the" blank row visible, and sets the BackColor to W Hite (the rest of the rows are presented in a different color). This code can is easily adapted to allow your to render whatever format of blank row and 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's to create a running Total row. As the DataTable is looped through, a running total can being kept and that value inserted to the correct column in the Dat ATable. Instead of rendering a blank row in the DataGrid, it can is 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.