Groups | Blog | Home
all groups > asp.net datagrid control > august 2004 >

asp.net datagrid control : Refresh and/or Reindex a datagrid


et
8/29/2004 8:53:34 AM
How can I refresh a datagrid, or more precisely, insert a new row with a new
index # and reorder the remaining rows. In ItemDataBound, if the value of
the county column is different than the county in the previous row, I insert
a new row with the County name. Get the current Index
(e.Item.DataSetIndex), create the DataItem, set the new DataItem with a new
index, Create a TableCell. set the value of the TableCell to the new county,
add the control to the new index. The new row is created just fine, but
it's not displayed in the correct place. It inserts the first row just
fine, but then the next row it inserts is 2 rows off, the next inserted row
is 3 rows off, etc. If I add 2 to the item count instead of one, then the
first row is in the wrong place. I know I'm missing something! Thanks for
your help.

Dim intItemCount As Integer

strNewCounty = e.Item.Cells(2).Text

strPerson = e.Item.Cells(1).Text

If strNewCounty <> strPreviousCounty Then

Dim intNewIndex As Integer = e.Item.DataSetIndex

Dim dgItem As DataGridItem

dgItem = New DataGridItem(intNewIndex, intNewIndex + 1,
ListItemType.Item)

Dim dgCell As TableCell

dgCell = New TableCell

dgCell.ColumnSpan = 10

dgItem.Cells.Add(dgCell)

dgCell.Text = strNewCounty

dgDrillers.Controls(0).Controls.AddAt(intNewIndex + intItemCount,
dgItem)

strPreviousCounty = strNewCounty

intItemCount = intItemCount + 1

End If





kaarkey
8/30/2004 12:35:03 AM

Please change
dgDrillers.Controls(0).Controls.AddAt(intNewIndex + intItemCount, dgItem)
to
dgDrillers.Controls(0).Controls.AddAt(intNewIndex + 1 ,
dgItem)
and try


[quoted text, click to view]
Eliyahu Goldin
8/30/2004 10:29:23 AM
Doesn't look to me that you are on the right track. You are interfering with
the grid populating process and confusing item indexing. Why not to let the
grid bind itself, and then, in PreRender event, go through the items and
insert whatever you want?

Be aware that this is a theoretical advice, I don't have practical
experience with inserting rows after databinding before rendering.

Eliyahu

[quoted text, click to view]

AddThis Social Bookmark Button