Refresh and/or Reindex a datagrid

Discussion in 'ASP .Net Datagrid Control' started by et, Aug 29, 2004.

  1. et

    et Guest

    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
     
    et, Aug 29, 2004
    #1
    1. Advertising

  2. et

    kaarkey Guest

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


    "et" wrote:

    > 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, Aug 30, 2004
    #2
    1. Advertising

  3. 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

    "et" <> wrote in message
    news:%...
    > 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
    >
    >
    >
    >
    >
    >
     
    Eliyahu Goldin, Aug 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brian lanning
    Replies:
    0
    Views:
    611
    brian lanning
    Jul 29, 2003
  2. ravi sankar

    datagrid operation and refresh

    ravi sankar, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    382
    ravi sankar
    Aug 26, 2003
  3. Per Magnus L?vold
    Replies:
    1
    Views:
    5,166
    John C. Bollinger
    Oct 8, 2004
  4. Pkenty
    Replies:
    0
    Views:
    412
    Pkenty
    May 28, 2004
  5. Jawahar Rajan
    Replies:
    1
    Views:
    303
    Adrienne
    Oct 1, 2003
Loading...

Share This Page