Filter Won't Take Variable Or Am I Missing A Quotation Mark Somewhere??

Hi,

I have a section of code that basically should apply filter to my data set that is bound to a listbox. Applying the filters works great for every one that uses a string for a filter, for example...

If e.Node.Text = "Operating System" Then
Me.lblTopic.Text = e.Node.Text
MyBindingSource.Filter =
"Topic='Operating System'"
End If

But I have one section that the filter will be based upon the hard drives on a user's computer and I wanted the code to be something like...

Dim getDrives4Fltr As System.IO.DriveInfo()
getDrives4Fltr = System.IO.DriveInfo.GetDrives
For Each DriveInfo4Fltr As System.IO.DriveInfo In getDrives4Fltr
If DriveInfo4Fltr.DriveType = IO.DriveType.Fixed Then
If e.Node.Text = DriveInfo4Fltr.Name Then
Dim DriveFilterString As String = Strings.Left(DriveInfo4Fltr.Name, 1) & " Drive"
Me.lblTopic.Text = DriveFilterString
MyBindingSource.Filter =
"Data2='DriveFilterString'"
Exit For
End If
End If
Next

The code runs through and the DriveFilterString variable is definitely evaluating to "C Drive" (I even added a quick If Then test to ensure that DriveFilterString = "C Drive" and it comes back as correct). But nothing gets returned as a result of the filter.

But if the code is...

Dim getDrives4Fltr As System.IO.DriveInfo()
getDrives4Fltr = System.IO.DriveInfo.GetDrives
For Each DriveInfo4Fltr As System.IO.DriveInfo In getDrives4Fltr
If DriveInfo4Fltr.DriveType = IO.DriveType.Fixed
Then
If e.Node.Text = DriveInfo4Fltr.Name
Then
Dim DriveFilterString As String = Strings.Left(DriveInfo4Fltr.Name, 1) &
" Drive"
Me.lblTopic.Text = DriveFilterString
MyBindingSource.Filter =
"Data2='C Drive'"
Exit
For
End
If
End
If
Next

Everything then works great and the filter applies correctly. I'm not sure if it cannot be done using a variable or if I'm just missing something stupid. I can't find anything on this!!

Any help would be appreciated.

Thanks.

-- Jim



Answer this question

Filter Won't Take Variable Or Am I Missing A Quotation Mark Somewhere??

  • Noam

    hi,

    i'm happy that your code working now, i have suggestion for you even though instead of typing all the filters one by one for each node you have i guess you can use a sub to do that something like that

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

    'you can use either afterselect eventhandler or mouse click , mouse click is better even

    MyBindingSource.filter = String.Format("Data='{0}'", e.Node.Text.ToString())

    End Sub

    hope this helps



  • shart44

    Never mind... after all that looking for an answer, I guess I just needed a break... I figured it out. The line needs to be:

    MyBindingSource.Filter = "Data2='" & DriveFilterString.ToString & "'"

    And now everything works perfect!

    -- Jim


  • Filter Won't Take Variable Or Am I Missing A Quotation Mark Somewhere??