Autogenerated Datagrid columns rebinding problem on postback

Discussion in 'ASP .Net Datagrid Control' started by John Walsch, Jul 12, 2004.

  1. John Walsch

    John Walsch Guest

    Just to give a brief background what I'm trying to do, is I have a
    DataGrid with 2 static columns, and the rest are autogenerated (which
    have checkboxes). Everything works fine until I try to UPDATE edited
    values. Basically in my Page_Load section I have:

    If Not IsPostBack Then
    End If

    where BindData binds the data to the DataGrid. So when I EDIT the
    DataGrid row, and then click on UPDATE, it calls my defined function.
    But when I get to that function, my autogenerated columns no longer
    exist, in which case I get the "System.ArgumentOutOfRangeException:
    Specified argument was out of the range of valid values. Parameter
    name: index" error when it tries to access the values of the
    checkboxes (in a for loop):

    For i As Integer = 2 To intColumnCount-1
    aDataTable.Rows(MyGrid.EditItemIndex)(i) =
    CType(e.Item.Cells(i).Controls(0), CheckBox).Checked

    Basically instead of my Column Count (at the time when I get to the
    for loop) in the DataGrid to be 6, it only returns 3 (the 2 static
    columns that I defined and the EditCommandColumn).

    Just to test it, I commented out the
    CType(e.Item.Cells(i).Controls(0), CheckBox).Checked part in the for
    loop and substituted with a static value (true), and also I commented
    out the BindGrid() function that was occurring at the end of my
    EditButtonClick function, and the page only displayed the DataGrid
    with the 2 static columns.... :(

    Ok, so to me it seemed like the DataGrid needed to be bound again
    (which should not be the case, and here is where I need help), so in
    the Page_Load section I changed my code to just BindData() without the
    If Not IsPostBack statement. And what do you know, it worked, and I
    was able to edit my Values.... But then I tried to edit the data in
    the static columns, and the CType(e.Item.Cells(1).Controls(0),
    TextBox).Text would return the OLD value, not the new one. So I put
    the if Not IsPostBack statememnt back in the Page_Load section and now
    I get the new values from the two static columns, but as mentioned
    above I get that other error. :(

    Did anybody experience that problem before? If so do you know what the
    problem could be, and how can it be easily fixed?

    Thank You,
    John Walsch, Jul 12, 2004
    1. Advertisements

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. MS
  2. MS
  3. Luis Esteban Valencia
    Luis Esteban Valencia
    Jan 11, 2005
  4. MikeG

    Sorting a datagrid with autogenerated columns

    MikeG, Jun 23, 2004, in forum: ASP .Net Datagrid Control
    Jun 23, 2004
  5. John Walsch
    John Walsch
    Jul 9, 2004

Share This Page