Read a Cell from a SharePoint 2010 GridView

I have a SharePoint 2010 Visual Web Part with a GridView control.  I bind it to a data table.  I have added a checkbox column and in code on a button click I can detect which row has that checkbox checked.

However,  I cannot access the other column values for the row.  I know they're there as I can see them in the watch window,  but I don't know how to read the value back into a string variable.

In the Watch expression I have the following:
foreach (GridViewRow Item in this.GridView1.Rows)
            {
                if (bOnlyCheckedRows)
                {
                    CheckBox cb2 = (CheckBox)Item.FindControl("chkRead");

                    if (cb2.Checked)
                    {                  
                        String Email = Item.Cells[3].Controls[0];  <==== this isn't allowed although I see the email value in the watch window
                        nextRecipient = Email;
                        acntList.Add(nextRecipient);
                    }
                }
                else
                {
                    String Email = Item.Cells[3].Controls[0];  <==== this isn't allowed although I see the email value in the watch window
                    nextRecipient = Email;

                    nextRecipient = Email;
                    acntList.Add(nextRecipient);
                }
            }

So,  I see the needed value in the Watch window expression: Item.Cells[3].Controls[0],  but haven't a clue how to assign it to my variable for usage within the code.

This should be trivial, but has me stumped.

Watch Window
LVL 1
Howard BashSenior Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

colly92002Commented:
Looks like you are casting from a control type to string, when you actually need the Text property of the control.
 
change:
String Email = Item.Cells[3].Controls[0];

to:
Control control = Item.Cells[3].Controls[0];  //create a control to get this item of the control array
String Email = ((TextBox)control).Text;  //cast control to a Textbox to easily get the .Text property

You can do this all on one line just as easily but I think the above makes it more obvious.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.