Refresh and/or Reindex a datagrid

E

et

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
 
K

kaarkey

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

Eliyahu Goldin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top