Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

How to read data from GridView cells using VB in code behind

Hi,

I have a gridview with checkboxes for making multiple selections. Upon submit I loop through the gridview to see which records were checked. When I find a checked record I want to read the record Id field and write it to another table....so far my code for the button click looks like below, but I am getting syntax error messages. VWD2010 is complaining about the line beginning qmsr_id = part of where I try to read the cell. Any help appreciated:

 
Dim nChecked As Integer
        Dim qmsr_id As Integer

        nChecked = 0
        For i As Integer = 0 To GridView1.Rows.Count - 1
            Dim row As GridViewRow = GridView1.Rows(i)
            Dim id As String = row.Cells(0).Text
            Dim chkSelect As CheckBox = DirectCast(row.FindControl("chkselect"), CheckBox)

            If chkSelect.Checked = "True" Then

                nChecked = nChecked + 1

                qmsr_id = Convert.ToInt32(GridView1.Rows[i].Cell[0])

            End If

        Next

        Label1.Text = nChecked

        Label2.Text = qmsr_id

Open in new window


0
Strawdog101
Asked:
Strawdog101
  • 3
1 Solution
 
ingriTCommented:
Why don't you use the variable you set above the If in

Dim id As String = row.Cells(0).Text

qmsr_id = Convert.ToInt32(id)
0
 
joshbulaCommented:
The problem might be that you don' t have the ".Text" on that line that starts with qmsr_id=

ingriT's suggestion should work.  

It will only be returning the last one that was checked, though.  Is that what you want, or do you want to add it to an array of ID's that were checked?
0
 
Strawdog101Author Commented:
Joshbula,

You mean like this:
 qmsr_id = Convert.ToInt32(GridView1.Rows[i].Cell[0].text) ?

Open in new window


VWD is still complaining that: comma, ')', or a valid expression continuation expected.

Once I get that solved, I need to then write each instance of qmsr_id to another table

Thanks.
0
 
Strawdog101Author Commented:
The only way I could get this to work was:

            Dim chkSelect As CheckBox = DirectCast(row.FindControl("chkselect"), CheckBox)
            Dim qmsrid As Integer

            If chkSelect.Checked = "True" Then
  Try
                qmsrid = Convert.ToInt32(txtQmsrid)
                Catch ex As Exception
                End Try
0
 
Strawdog101Author Commented:
I am sure there are many ways of doing this, but this is how I solved it in the end.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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