Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2003-11-28
4
2,238 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

838 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