Solved

Gridview RowEditing Can't FindControl

Posted on 2013-02-01
5
1,251 Views
Last Modified: 2013-02-07
When entering Edit mode on my GridView I place the focus on a field with the FindControl method. All is well, but if I move to page #2 my code crashes, (ArgumentOutOfRangeException).

What am I doing wrong??

 Private Sub gv_Gifts_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv_Gifts.RowEditing
       
        If gv_Gifts.EditIndex = e.NewEditIndex Then
            e.Cancel = True
            Exit Sub
        End If

        gv_Gifts.EditIndex = e.NewEditIndex
        LoadOffers()

        gv_Gifts.Rows(e.NewEditIndex).FindControl("giftTitle").Focus()

    End Sub

Open in new window

0
Comment
Question by:rcl58
  • 2
  • 2
5 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 38845308
Check:
http://www.experts-exchange.com/Microsoft/Server_Applications/Q_24474303.html

The e.NewEditIndex value is relative to gv page you are displaying, thus you need to calculate the correct Index. (Insert at line 2)
Dim correctIndex As Integer = e.NewEditIndex
If (GridView1.PageIndex > 0) Then
  correctIndex = e.NewEditIndex + (GridView1.PageSize * GridView1.PageIndex
EndIf

Open in new window

Then use correctIndex in all your code lines 3 to11. (instead of e.NewEditIndex)
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38845486
You need to rebind your GV before getting the control. So is your LoadOffers() rebinding the GV?

gv_Gifts.EditIndex = e.NewEditIndex
gv_Gifts.DataBind()
0
 

Author Comment

by:rcl58
ID: 38859213
Oops, the accepted solution is not working , same problem.

After going to page 2 and entering edit on the first row
e.NewEditIndex = 10
.PageSize = 10
.PageIndex = 1

Suggestion "correctIndex = e.NewEditIndex + (GridView1.PageSize * GridView1.PageIndex)" result in a value of 20.

Also the binding code change suggestion didn't help:

gv_Gifts.EditIndex = e.NewEditIndex
gv_Gifts.DatBind()

I'm still lost and may have to repost because I already accepted the solution.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 38861833
is the gridview binding in markup? I f no, please post which event(s) are binding the gridview dynamically.
Can you post the exception details?
Also if you can post the contents of gv_Gifts.Rows before the exception is thrown. I am interested in the count property whether Rows(0) is the actual first row or is the 11 row.
NOte: 1) IF you can include before/after values of my suggested code changes will be great.
2) No need to ask a new question, just request the moderator to reopen the question.
0
 

Author Comment

by:rcl58
ID: 38865596
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

20 Experts available now in Live!

Get 1:1 Help Now