[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2269
  • Last Modified:

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 !!!

0
cajunworks
Asked:
cajunworks
1 Solution
 
mmarinovCommented:
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
0
 
rovermCommented:
Try something like this:

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

So access cell 0 inside cell 2.
0
 
David H.H.LeeCommented:
cajunworks,
The easier way to solve the problems is use FindControl function.
eg:
tb =CType(e.Item.FindControl("TextBoxNameInEdit"), TextBox)

'You just need to put the control name when you defined in EditItemTemplate
eg:
       <EditItemTemplate>
         <asp:TextBox ID="TextBoxNameInEdit" Columns="4" Runat="server" />
       </EditItemTemplate>

Regards
x_com
0
 
NaliniPCommented:
what X_COM said is correct.. you can use the <EDITITEM TEMPLATE> to put a textbox while editing..its great

Nalini
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now