Link to home
Start Free TrialLog in
Avatar of cajunworks
cajunworks

asked on

Asp .net CType(e.Item.Cells(2).Controls(0), TextBox) ----> Error: Out of bound

I am trying to put a edit button in the DataGrid, so when clicked would display a textbox to edit the record.

Here is the code:
------------------------------------------------------------------------------------------
   Private Sub MyDG_EditCommand(ByVal source As Object, ByVal e As  System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyDG.EditCommand

        MyDG.EditItemIndex = e.Item.ItemIndex()
        MyDG.DataBind()

        Dim key As String = MyDG.DataKeys(e.Item.ItemIndex).ToString

        Dim CatName, CatImage, CatThumb, CatDesc As String
        Dim tb As TextBox


        tb = CType(e.Item.Cells(2).Controls(0), TextBox) 'Here is where I am getting ERROR
        CatName = tb.Text

-------------------------------------------------------------------------------------------------------

It gives me following ERROR:
-------------------------------------------------------------------------------------------------------
Specified argument was out of the range of valid values. Parameter name: index

Line 149:        tb = CType(e.Item.Cells(2).Controls(0), TextBox)
-------------------------------------------------------------------------------------------------------

I Debug the program and try to get the values and it gave me following result:

?e.item.cells.count  ----------------------> 6
?e.item.cells.controls.count -------------> 0      'I think here is the problem because it is zero

But I don't know what does it mean and how would I solve it.

Please Help !!!

Avatar of mmarinov
mmarinov

How do you write you EditTemplate in your HTML ? Can you  post the code ?
The problem is realy in e.item.cells.controls.count because there are no controls int the current cell

B..G
Try something like this:

tb = CType(e.Item.Cells(2).Cells(0).Controls(0), TextBox)

So access cell 0 inside cell 2.
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what X_COM said is correct.. you can use the <EDITITEM TEMPLATE> to put a textbox while editing..its great

Nalini