Solved

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

Posted on 2003-11-28
4
2,225 Views
Last Modified: 2009-01-08
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
Comment
Question by:cajunworks
4 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 9837847
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
 
LVL 12

Expert Comment

by:roverm
ID: 9838034
Try something like this:

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

So access cell 0 inside cell 2.
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 250 total points
ID: 9841077
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
 
LVL 2

Expert Comment

by:NaliniP
ID: 9841499
what X_COM said is correct.. you can use the <EDITITEM TEMPLATE> to put a textbox while editing..its great

Nalini
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now