Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build a string for a filter

Status
Not open for further replies.

Bodhi147

MIS
Jan 24, 2003
17
US
I am checking the state of each row in a checked list box. If the item is checked I would like to filter a datatable by that item. I don't know how to handle if more than one item is checked in the list box. I am trying to build a string value for the filter but it will only filter by the last item selected. My problem is building the string with multiple variables inside the for/next.

Thanks for the help

iFurnaceCount = chkFilterFurnaces.Items.Count()

For z = 0 To iFurnaceCount - 1
iFurnaceName = chkFilterFurnaces.Items(z)
x = chkFilterFurnaces.GetItemCheckState(z)
If x = 1 Then

strFilter = "Furnace = " & iFurnaceName & " OR Furnace = " & iFurnaceName
gdtTentative.DefaultView.RowFilter = strFilter

End If
Next z
 
You can build your string in your loop like this


For z = 0 to iFurnaceCount -1
iFurnaceName = chkFilterFurnaces.Items(z)
x = chkFilterFurnaces.GetItemCheckState(z)
If x = 1 Then
If strFilter = "" Then
strFilter = "Furnace = " + iFurnaceName
Else
strFilter = strFilter + " OR Furnace = " + iFurnaceName
End If
End If
Next z

gdtTentative.DefaultView.RowFilter = strFilter


I did change your & to + because & isn't recognized in .Net; but I didn't verify the rest of the code.
 
I did change your & to + because & isn't recognized in .Net
Isn't it? I don't recall it ever not being recognised...

Also, you should use a StringBuilder instead of appending text to the end of an existing string.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Strings are static objects. So when you use

MyString = MyString & "some text"

You take create a new string in memory with the value "some text", then create another string in memory that conatins MyString & "some text" then (not positive on this part) either that value is copies to the memory location of the MyString variable, or the pointer for MyString is updated to reflect the location new MyString value.

StringBuilders work more like linked lists. Which means doing StringBuilder.Append("Some text") only creates one new string in memory ("Some Text") and the StringBuilder updates the memory addresses that it contains. Much more efficient.

-rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top