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

Posted on 2011-05-02
Last Modified: 2012-05-11

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


        Label1.Text = nChecked

        Label2.Text = qmsr_id

Open in new window

Question by:Strawdog101
    LVL 6

    Expert Comment

    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)
    LVL 9

    Expert Comment

    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?

    Author Comment


    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


    Accepted Solution

    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
                    qmsrid = Convert.ToInt32(txtQmsrid)
                    Catch ex As Exception
                    End Try

    Author Closing Comment

    I am sure there are many ways of doing this, but this is how I solved it in the end.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now