Get a value from a gridview cell


Newbie question:

I cannot get a value from a cell in a gridview. The gridview loads properly. The first column is a template column with a checkbox. I am able to check whether the user selects the checkbox or not. But I am unable to get any vaule from any other cells in that row.

I try to use:   gridview1.SelectedRow.Cells(2).Text    to get the value I want.
I get a NullReference Exception was unhandled by user code error.

The gridview has 6 columns. The first is the checkbox column and the other 5 all have data populated in them.
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.

First make sure that you have the gridview selectionmode property set to FullRowSelect. This is what the code should look like in c# (dunno if this part will really help you or not):

Open in new window

Try this
For iRow As Integer = 0 To GridView1.Rows.Count - 1
    Dim chkBox As New CheckBox()
    chkBox = DirectCast(GridView1.Rows(iRow).FindControl("mycheckbox"), CheckBox)
    If chkBox.Checked Then
        string str = Gridview1.Rows(iRow).Cells(1).ToString()
    End If

Open in new window

HainKurtSr. System AnalystCommented:
put a breakpoint on this line
add this to watch "gridview1.SelectedRow.Cells", browse all properties to understand it

and open immediate window and put


Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jvoconnellAuthor Commented:

I get "Referenced object has a value of 'Nothing' in the intermdiate window
HainKurtSr. System AnalystCommented:
when you add this to watch


you get nothing... means no row is selected...
HainKurtSr. System AnalystCommented:
maybe you should use this

Dim index As Integer = gridview1.EditIndex
Dim row As GridViewRow = gridview1.Rows(index)

then use

jvoconnellAuthor Commented:
Am I making an incorrect assumption that when the checkbox is checked that the row is "selected"?
HainKurtSr. System AnalystCommented:
HainKurtSr. System AnalystCommented:
also this one, similar to one above

basically they loop rows and check the checkbox to find the selectedrow...

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
HainKurtSr. System AnalystCommented:
also check this

to select a row with a check box, actually when you click checkbox you will do a post back to select that row...
just another approach...
jvoconnellAuthor Commented:
This is it!!!!   Thank you for all of the assistance. It is very much appreciated.
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

From novice to tech pro — start learning today.