Datagrid e.Item.Cells(1) argument was out of range

D

Del

I'm creating a dynamic datagrid using Dim objbc As New BoundColumn, etc. The
datagrid is populating correctly. I am also using first column of grid as
'edit', on clicking this the update link is displayed and textboxes are
returned. All good so far.

When I change the values in the text box and click update, I keep getting:
Specified argument was out of the range of valid values. Parameter name: index

for this line
tb = CType(e.Item.Cells(1).Controls(0), TextBox)

I've checked the content of e.item.cells and it is empty, why????

If I create the datagrid and bind it directly to a dataset then everything
works, however I want to create the dataset, etc. completely dynamically.
What am I missing? I've seen much correspondence of others getting the same
issue, but there are no answers. After much searching I did find a post
suggesting that the issue is due to viewstate, and that the binding of the
columns should be done on Page_Init and not Page_Load. Is this the solution
I'm looking for?
 
D

Del

Please ignore my question, a colleague has assisted me in solving the
problem. The issue lay around the way I was getting the data and binding it
to the grid.
 
D

Del

During postback the grid was being destroyed, and this was leading to the
error. I broke down my code for getting and binding the data to the grid, to
2 separate routines and undertake checks during postback to see whether the
session with the dataset still exists, if it does then rebind the data to the
datagrid.

Sample Code:

If Not IsPostBack Then
PopDropDown() 'populate drop down
Else
Dim ds As System.Data.DataSet
ds = CType(Session("DSforDG"), DataSet)
If Not ds Is Nothing Then 'reset dataset if it's empty due to
change in dropdown
If ds.Tables.Count > 0 Then
DS11 = ds
BindData()
End If
End If
End If

The changing of item in the combobox calls the GetData routine and stores
the data in the Session.

Hope this helps.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top