VS 2010 [RESOLVED] datagrid text box search-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] datagrid text box search

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,315

    Resolved [RESOLVED] datagrid text box search

    I have a text box setup to filter a datagrid. Work just fine, but if I search for something that is not there (empty results) it comes back with an error

    "Object reference not set to an instance of an object."

    on the bolded line. How to fix that error?

    Code:
    Private Sub LicensesBindingSource_CurrentChanged(ByVal sender As Object,
                                              ByVal e As EventArgs) Handles LicensesBindingSource.CurrentChanged
    
            'Make sure the grid has columns, which it won't when the BindingSource is first bound.
    
            If Me.DataGridView1.ColumnCount > 0 Then
                'Get the underlying DataRow that corresponds to the selected row in the grid.
                Dim row As DataRow = DirectCast(Me.LicensesBindingSource.Current, DataRowView).Row
                'Only allow editing if the row is detached (created but not yet added to the table)
                'or added (added to the table since the last AcceptChanges call).
                Me.DataGridView1.Columns(0).ReadOnly = Not (row.RowState = DataRowState.Detached OrElse
                                                        row.RowState = DataRowState.Added)
    
            End If
        End Sub

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,369

    Re: datagrid text box search

    Test for nothing before casting like:-
    vbnet Code:
    1. If Me.LicensesBindingSource.Current IsNot Nothing Then
    2.             Dim row As DataRow = DirectCast(Me.LicensesBindingSource.Current, DataRowView).Row
    3.             'Only allow editing if the row is detached (created but not yet added to the table)
    4.             'or added (added to the table since the last AcceptChanges call).
    5.             Me.DataGridView1.Columns(0).ReadOnly = Not (row.RowState = DataRowState.Detached OrElse
    6.                                                     row.RowState = DataRowState.Added)
    7. End If

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,315

    Re: datagrid text box search

    perfect, thank you Niya, I was close. I had that but was checking for the wrong item.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.