-
Jan 13th, 2012, 02:51 PM
#1
Thread Starter
Frenzied Member
[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
-
Jan 13th, 2012, 03:06 PM
#2
Re: datagrid text box search
Test for nothing before casting like:-
vbnet Code:
If Me.LicensesBindingSource.Current IsNot Nothing Then 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
-
Jan 13th, 2012, 03:10 PM
#3
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|