how do I convert gridview checkbox to boolean

chandanchoubey
chandanchoubey used Ask the Experts™
on
I have check box in a column on gridview, when I try to use .checked method it gives me NullReferenceException it says, "Object reference not set to an instance of an object."

How do I use check box on a grid view?
Dim MyChkIsSendSMS As New CheckBox
 MyChkIsSendSMS = CType(DgBulkSMS.FindControl("ChkIsSendSMS"), CheckBox)
 
                If MyChkIsSendSMS.Checked Then

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:
It depends when and which checkbox value you want to get...
1:For all Checkbox..
You have to loop through each row of GV and then find control....Like below:

For Each row As GridViewRow In DgBulkSMS.Rows
    If row.RowType = DataControlRowType.DataRow Then
        Dim chb As CheckBox = DirectCast(row.FindControl("ChkIsSendSMS"), CheckBox)
        If chb IsNot Nothing Then
            If chb.Checked Then
                'do whatever you want
            End If
        End If
    End If
Next

2: Could be individual checkbox of a particular row...

So provide what is it that you want to do....

Author

Commented:
Thanks for your response, actually I am trying to send bulk SMS. the gridview has four columns, checkbox,  name, PIN,  Phone number. There is button to send SMS to all people whose check box is checked.
Most Valuable Expert 2012
Top Expert 2014

Commented:
As i suggested in your other question, try using a Boolean column in the dataset instead of a checkbox in the grid.
Top Expert 2013
Commented:
---> There is button to send SMS to all people whose check box is checked.
In that case on your button Click, you iterate through the  GridView rows and check if the checkbox is clicked..sent SMS....
--Add this code to Button Click Event handler
For Each row As GridViewRow In DgBulkSMS.Rows
    If row.RowType = DataControlRowType.DataRow Then
        Dim chb As CheckBox = DirectCast(row.FindControl("ChkIsSendSMS"), CheckBox)
        If chb IsNot Nothing Then
            If chb.Checked Then
                'Considering you have 4 columns and 1=checkbox,2=name and so no
                Dim name As String = row.Cells(1).Text
                Dim PIN As String = row.Cells(2).Text
                Dim phone As String = row.Cells(3).Text
                'SEND SMS
         End If
        End If
    End If
Next

Author

Commented:
Thanks guru Sami. You were bang on target!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial