• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

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.
0
jvoconnell
Asked:
jvoconnell
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now