Generate a blank record in DataGrid

zhaozj2021-02-16  77

Generate a blank record in DataGrid

HBZXF (A Ho) www.cnblogs.com/hbzxf

?

In the application, I need to add a blank line after 10 records in a DataGrid, and we can simply modify the DataTable, and write some code in the itemDatabase event in the DataGrid. The following article will explain how to do it. To this. 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 .........................

Add empty line to DataTableAdding Blank Rows to the dataatable

?

Add an empty room to add a empty cycle to use a simple loop to do 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 can not use a for loop with a counter as the upper bound of the loop is re-evaluated on every iteration not! 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 TRABLES (S) UpperBound = DS.TABLES 0) .rows.count ???? While count <= Upperbound ???????? if repeat = 1 THEN ???????????? DRBLANK = DS.TABLE s (0) .newrow ???????????? DRBLANK (0) = -count ???????????? DS.TABLES (0) .Rows.Insertat (DRBLANK, COUNT 1) ???????????? count = 1 ???????????? Upperbound = 1 ???????????? RepeatCount = 10 ???????? ELSE ???????????? rebeatcount - = 1 ?????????g ??????? a count = 1?? ?? End while ???? Return Dsend Function

Reflected empty lines to 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 = list ItemType.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 IFEND 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.

转载请注明原文地址:https://www.9cbs.com/read-24243.html

New Post(0)