Solved

Got error when retrieving the current text value out of a gridview control during gridview Updating event.

Posted on 2014-12-21
11
238 Views
Last Modified: 2014-12-22
Hi, I'm using vs2012.
I used the code form msdn, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting%28v=vs.110%29.aspx

However, I'm getting out range error in run time.    Attached is my code snipet.  The error happens at
 "var sam = ((TextBox)(editRow.Cells[1].Controls[0])).Text;"

I think that the code is taking the row that is being edited (editRow is correct when I examine the property).  Cell 0 I think is the Edit button on my grid, it shows as the first column.  So cells[1] is the column that I want.  Examine the property of Cells show it has count of 6 and that is correct, 1st one is the Edit button generated by gridview control  however, the error is with Controls[0] being out of range.  

In the aspx file, the 1st cell is a boundfield, code as below
<asp:BoundField DataField ="sAMAccountName" HeaderText="User ID" ReadOnly="True" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle"></asp:BoundField>

Appreciate help here to fix the problem.  Thank you.

I add editRow.Cells to the Watch and there is a cells[1] object.  But I don't know how to expand to see where is the Controls[0] because that's where I get the error.  I'm attaching the screen shot for this too.

further examination by adding to the Watch panel, I found the error is caused because Cells[1].Controlls.count is 0.  I'm also attaching a screen shot for this.  Why is that so?  It does display the data that it's bound to.  How can I fix this?
Thank you.
snipet.txt
grid-err-1.png
grid-err-2.png
0
Comment
Question by:lapucca
[X]
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
  • 7
  • 4
11 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40511976
Please post your grid view mark up and/or sample page you are using. (and any other code setting change that differs from sample MSDN)
0
 

Author Comment

by:lapucca
ID: 40512006
Attached is the gridview snipet code from the aspx page.
rowUpdating.txt
grid.txt
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40512121
Any particular reason for not using Text property:
            editUser.GivenName = editRow.Cells[2].Text;
            editUser.Surname = editRow.Cells[3].Text;

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:lapucca
ID: 40512133
Because that's the example form msdn, in the row upating event.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting%28v=vs.110%29.aspx 

It does work for all other columns except the User ID column that I get the out of range error.  It's odd that it shows no controls at all for this cell.  I am able to get data back for other columns except this one and the last column which I think it's a date conversion problem.  Any idea on how t fix this?
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40512242
Your user id column is not being mapped into the grid mark up, thus it is not visible and it does not have an associated control.

For data conversion, check if my  proposed solution fix your issue.
0
 

Author Comment

by:lapucca
ID: 40512321
The userid column is  displaying correct data that is bind to the gridview.  If it isn't mapped then how can it display the data? How to tell that it isn't mapped to the grid?  Thank you.
0
 

Author Comment

by:lapucca
ID: 40512327
In the gridview smart tab I also see this column configured properly already.  Attached is the screen shot.
smart-Tab.png
0
 

Author Comment

by:lapucca
ID: 40512334
This column is the only ReadOnly column.  I set ReadOnly property to false and now I can retrieve the value.  How to get around this?  I don't want user to edit this column but user id is the key for me to find which record in the collection the user is editing.
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40512340
Just use editRow.Cells[1].Text to access the value.
0
 

Author Comment

by:lapucca
ID: 40512359
editRow.Cells[1].Text returns an empty string, ""
0
 

Author Closing Comment

by:lapucca
ID: 40513962
That works perfectly but I had to first change back that column to Read Only.  Thank you Miguel.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 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