I am using this code in C# as shown below, to view the students first name.

studentsBindingSource.filter = "[First Name] like '%" + txtFirstName.text + "%'";

This code is very useful and working well.

If i input at a textbox for example, a letter [M] or abbreviations, all the students with letter M will be displayed, so no problem with that.

But how about if i input an abbreviated name with spaces for example [M a] or [A u], [Ad Us], etc., that displays a two word name for having it specific. For this example a named with "M a" will be displayed like "Mary Ann", Mark Andrew and so on.

So i need help to solve this problem, coz i'm having trouble for this, coz everytime i use an abbreviation that uses spaces resulted to a no display data grid view.

thanks...

Recommended Answers

All 3 Replies

studentsBindingSource.filter = "[First Name] like '" + txtFirstName.text + "%' AND [Last Name] Like '" + txtLastName.Test + "'%";

I assume that you seperated the First Name column and the Last Names column.

If not then you would need to play with Substring.

If you show some more examples I can help you.

Unfortunately the wildcard symbol (%) is only allowed at the start and/or end of the filter string.
You would need to implement your own logic to apply the filter the way you have stated.
Something like:

string Value = "M a";
            string filter = string.Empty;
            if (Value.Contains(" ")) //check for space in filter value
            {
                string[] Values = Value.Split(' '); //seperate each value

                filter+="([First Name] like '" + Values[0] + "%') AND"; //check first name begins with first letter
                for(int i = 1; i<Values.Length;i++) //for each additional set of values
                {
                    filter += "([First Name] like '% " + Values[i] + "%') AND"; //add additional filter condition
                }
                filter = filter.Substring(0, filter.LastIndexOf("AND") - 1); //remove final AND from filter
            }
            else
            {
                filter = "[First Name] like '%" + Value + "%'";
            }

The above code would match "Mark andrew" or "Mary Lilly Anne".
You can be as specific as you like with the way you build the filters; string each condition together with an AND to ensure all parts are matched, or repalce AND with OR to make each match optional etc.

Ok thanks my problem was solved...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.