Newbie Rowfilter question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm fairly new to vb .net and was playing around with a sub to filter my dataview
like "ProductName LIKE '%" & strSearchString & "%'" , ProductName is the actual name of
the column of course, except i want pass the columnname as
a variable (strColumnName), but I can't seem to get any expression to work. If I just put the
Varible in ' ' it doesnt search correctly, but everything else gives "column not found error"
 
is it something like this you want to do?


dim columnName as String
dim searchString as String

dim filter as String
filter = columnName + " LIKE '%" + searchString + "%'"

if you set columnName to "ProductName" the result filter will be
"ProductName LIKE '%" & strSearchString & "%'"


dominique
 
Have you tried:

Dim columnName As String = "ProductName"
Dim searchString As String = "A Product"

Dim view As New DataView
view.RowFilter = String.Format("{0} Like '%{1}%'", columnName,
searchString)

Note I find using String.Format here is easier to read then string
concatenation.

Hope this helps
Jay
 
hum, strange I didn't use String.Format in my example
I normally never use +'s for string concatenation in my code...
 
Hi Jay B,

So you can see how taste can be different, I find the String.Format absolute
unreadable, but you know I find that also from the regulair expressions.

I prefer
view.RowFilter = columnName & " LIKE '%" & searchString & "%'"

Above

View.RowFilter = String.Format("{0} Like '%{1}%'", columnName,
searchString)

With that I will not say that there something is wrong with your solution,
only that a lot of things in writing are a personal taste

However you did use exactly the right words, not "better" but "better to
read".

Cor
 
Cor
However you did use exactly the right words, not "better" but "better to
read".
I suspect something is being lost in the translation here, as I stated: I
find String.Format easier to read, which is personal opinion.

I did not state that String.Format is easier to read, as that is conjecture.

Hope this helps
Jay
 
Dominique,
What's stranger is we used the same variable names! ;-)

When using Concatenation I normally use & (concatenation operator) over +
(addition operator) to avoid the operands being converted to numbers and
added...

dim columnName as String
dim searchString as String

dim filter as String
filter = columnName & " LIKE '%" & searchString & "%'"

Just a thought
Jay
 
Hi Jay,
I suspect something is being lost in the translation here, as I stated: I
find String.Format easier to read, which is personal opinion.

As that I readed it and I tried to show extra to the OP, that this was a
personal opinion and that this was not a case of better or worse (And also
not a wrong opinion).

Was exactly correct written by you (as always, but we know).

(And in the same time I could show that "&" instead of the "+" from
Dominique)

:-))

Cor
 
Cor,
As I stated, something may be lost in the translation.

You stated:
"So you can see how taste can be different"

In a post directed to Me, which to me implies that you don't think I don't
know the difference between String.Format & Concatenation. I assure you I
do! :-|

If you were attempting to show something to the OP, it would have sounded
"better" to me to have been addressed to the OP and not me. Then I would not
have wondered what you were talking about...

Thanks for understanding
Jay
 
Doh!
Double negative:
In a post directed to Me, which to me implies that you don't think I don't
know the difference between String.Format & Concatenation. I assure you I
do! :-|
Should be "implies that you think I don't know the difference"...

Any way, life goes on

Jay
 
should maybe learn to use & for the few time I use string concatenation that
way
+ is default for me, a lot of java background ;-)
 
Back
Top