How to use LIKE on int column?

I have a text box that users can type in a PersonID (all integers) and get a match of IDs in a datagrid.  At least that is the concept.  The problem is that I keep getting errors related to "Cannot perform LIKE operation on System.Int32 and System.String" or something similar depending on how I rearrange the datatable column data type and the filter expression.

I'm currently changing the integer column to a string hoping it can use LIKE.

ds.Tables[0].Columns["PersonID"].DataType = System.Type.GetType("System.String");

The above throws an exception because the column datatype can't be modified after it is filled with data.  I'm using a data adapter to fill a data set so the table will always have data unless I get into creating the datatable from scratch, which I want to avoid.

Then I do this:
dt.DefaultView.RowFilter = "PersonID Like '" + txtPersonCode.Text.ToString() + "'";

and get the error.  I want a user to type in 101 and get results such as 1010, 10122, 1011.  How can I do this?

Thanks,
Brett
brettrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

brettrAuthor Commented:
One problem I see is that it seems the column datatype is never changed.

Brett
gregoryyoungCommented:
try ... using

Convert(PersonId, 'System.String') like ...

That will convert to a string then use the string in your like comparison.

Cheers,

Greg
brettrAuthor Commented:
Where exactly are you using Convert?

Thanks,
Brett
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

gregoryyoungCommented:
brettrAuthor Commented:
That isn't working and it kicks out my tablestyle.

dt.DefaultView.RowFilter = "Convert(PersonID, 'System.String') Like '" + txtPersonID.Text.ToString( ) + "'";
gregoryyoungCommented:
whats the exception? that looks valid at first glance.
brettrAuthor Commented:
No errors but blanks out all the rows and I just have the column headers from the under lying table.
gregoryyoungCommented:
is it possible it is just returning no rows?

brettrAuthor Commented:
No.  I type in 2 or 4.  There are many rows in the grid I see starting with a 2 or 4.  Then I execute the filter and nothing.
gregoryyoungCommented:
2 or 4 would only match 2 or 4 ... 2* or 2% would match any starting with 2 or 4

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
brettrAuthor Commented:
Ok, that goes but it jumps out of the tablestyle.  I get the right results but my style is no longer there.  Any ideas?
gregoryyoungCommented:
not off the top of my head without looking at the rest of your code ... the rowfilter has nothing to do with your tablestyles.
brettrAuthor Commented:
Could it be that since the column type has changed, that column no longer maps to the tablestyle and so can't find a matching tablestyle (b/c it is off by one column) at that point?
gregoryyoungCommented:
you arent changing the column type.

you are using a conversion in the rowfilter ... the columns should stay the same.
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
C#

From novice to tech pro — start learning today.