rowfilter of dataview or datagrid

I want the user to be able to type a last name and while typing the datagrid will change to display only the filtered data.  I got this far and it works with first letter but another one causes it to error when creating the dv    

Private Sub LastNameIn_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LastNameIn.TextChanged

        Dim dv As DataView = CType(DataGrid1.DataSource, DataTable).DefaultView()
        With dv
            .RowFilter = "[LAST NAME] like '" & LastNameIn.Text & "*'"
            .RowStateFilter = DataViewRowState.CurrentRows
        End With
        DataGrid1.DataSource = dv

    End Sub
MillkindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YZlatCommented:
what error does it give you?
MillkindAuthor Commented:
The following line of code is highlighted:
Dim dv As DataView = CType(DataGrid1.DataSource, DataTable).DefaultView()

And the error is:
An unhandled exception of type 'System.InvalidCastException' occurred in Post Office Registered Mail.exe

Additional information: Specified cast is not valid.
Éric MoreauSenior .Net ConsultantCommented:
Hi Millkind,

The DataSource of your DataGrid is set to a DataView by this line:
DataGrid1.DataSource = dv

A DataView cannot be casted as a DataTable. This line:
Dim dv As DataView = CType(DataGrid1.DataSource, DataTable).DefaultView()

Would then become:
Dim dv As DataView = CType(DataGrid1.DataSource, DataView)


Cheers!
amyhxuCommented:
   'I would set the datagrid's datasource to dataview all the time. What you are doing is to set it to datatable first and then change to dataview, which is causing the problem.

Declare a class level variable for dataview.
     Dim dv As DataView

When you first set your datagrid's datasource, set it to be the DataView.
     dv = New DataView(myDataTable)
     DataGrid1.DataSource = dv

When the last name is changed, you only need to modify the filter.
    Private Sub LastNameIn_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles LastNameIn.TextChanged

        dv.RowFilter = "[LAST NAME] like '" & LastNameIn.Text & "*'"
        dv.RowStateFilter = DataViewRowState.CurrentRows

    End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MillkindAuthor Commented:
Perfection thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.