Autogenerated Datagrid columns rebinding problem on postback

J

John Walsch

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
BindData()
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
Next

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
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top