Get a value from a gridview cell

Experts,

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.
LVL 1
jvoconnellAsked:
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.

Death259Commented:
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):
dataGridView1.SelectedRows[0].Cells[2].FormattedValue.ToString()

Open in new window

0
masterpassCommented:
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
Next

Open in new window

0
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

?gridview1.SelectedRow.Cells
?gridview1.SelectedRow.Cells.Count
?gridview1.SelectedRow.Cells(0).Text
?gridview1.SelectedRow.Cells(1).Text
?gridview1.SelectedRow.Cells(2).Text

0
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:
HainKurt:

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

gridview1.SelectedRow
or
?gridview1.SelectedRow

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

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

then use

row.Cells(2).Text    
0
jvoconnellAuthor Commented:
Am I making an incorrect assumption that when the checkbox is checked that the row is "selected"?
0
HainKurtSr. System AnalystCommented:
0
HainKurtSr. System AnalystCommented:
also this one, similar to one above
http://www.highoncoding.com/Articles/81_Selecting_Checkboxes_inside_GridView_Control.aspx

basically they loop rows and check the checkbox to find the selectedrow...
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
HainKurtSr. System AnalystCommented:
also check this

http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/6e29b371de01b72f?hl=en

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...
0
jvoconnellAuthor Commented:
This is it!!!!   Thank you for all of the assistance. It is very much appreciated.
0
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
ASP.NET

From novice to tech pro — start learning today.