GridView ChechBox check all in vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sushil349
    New Member
    • Oct 2013
    • 1

    GridView ChechBox check all in vb.net

    I am getting error as Object reference not set to an instance of an object.
    Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.

    Source Error:


    Line 251: ' .FindControl("c hk"), CheckBox)
    Line 252: If chk IsNot Nothing Then
    Line 253: chk.Checked = arr.Contains(gv All.DataKeys(i) .Value)
    Line 254:
    Line 255: If Not chk.Checked Then
    please help me


    Code:
     Private Sub GetData()
            Dim arr As ArrayList
            If ViewState("SelectedRecords") IsNot Nothing Then
                arr = DirectCast(ViewState("SelectedRecords"), ArrayList)
            Else
                arr = New ArrayList()
            End If
            Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
                        .Cells(0).FindControl("chkAll"), CheckBox)
            For i As Integer = 0 To gvAll.Rows.Count - 1
                If chkAll.Checked Then
                    If Not arr.Contains(gvAll.DataKeys(i).Value) Then
                        arr.Add(gvAll.DataKeys(i).Value)
                    End If
                Else
                    Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
                                                .FindControl("chk"), CheckBox)
                    If chk.Checked Then
                        If Not arr.Contains(gvAll.DataKeys(i).Value) Then
                            arr.Add(gvAll.DataKeys(i).Value)
                        End If
                    Else
                        If arr.Contains(gvAll.DataKeys(i).Value) Then
                            arr.Remove(gvAll.DataKeys(i).Value)
                        End If
                    End If
                End If
            Next
            ViewState("SelectedRecords") = arr
        End Sub
    
        Private Sub SetData()
            Dim i As Integer
            Dim var = DirectCast(ViewState("SelectedRecords") _
                                            , ArrayList)
            ' Dim s = DirectCast(ViewState("SelectedRecords") _
            '   , ArrayList)
            Dim currentCount As Integer = 0
            Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
                            .Cells(0).FindControl("chkAll"), CheckBox)
            chkAll.Checked = True
            Dim arr As ArrayList = DirectCast(ViewState("SelectedRecords") _
                                            , ArrayList)
    
            For i = 0 To gvAll.Rows.Count - 1
    
                Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0) _
                                                .FindControl("chk"), CheckBox)
                'Dim var = DirectCast(gvAll.Rows(i).Cells(0) _
                ' .FindControl("chk"), CheckBox)
                If chk IsNot Nothing Then
                    chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
    
                    If Not chk.Checked Then
                        chkAll.Checked = False
                    Else
                        currentCount += 1
                    End If
                End If
            Next
            hfCount.Value = (arr.Count - currentCount).ToString()
        End Sub
    Last edited by Rabbit; Oct 29 '13, 05:52 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Could be that the Cells property is nothing... Try using the FindControl method on the row instead:

    Code:
      Dim chk As CheckBox = DirectCast(gvAll.Rows(i).FindControl("chk"), CheckBox)
               
      If chk IsNot Nothing Then
        chk.Checked = arr.Contains(gvAll.DataKeys(i).Value)
        If Not chk.Checked Then
          chkAll.Checked = False
        Else
          currentCount += 1
        End If
      End If
    -Frinny

    Comment

    Working...