0

I am doing a filter on my DataGridView and I want that the user can search in all Columns through only one TextBox.

I want this behavior:

Name   |  Address          |  Telephone
Luis   |  Costa Rego       |   36595845
Paulo  |  Antonio Emmerich |   36595846

The user search: 3659584 and it shows:

Name   |  Address          |  Telephone
Luis   |  Costa Rego       |   36595845
Paulo  |  Antonio Emmerich |   36595846

but when he search: 3659584 Lu it shows:

Name   |  Address          |  Telephone
Luis   |  Costa Rego       |   36595845

To do so, I need to substring all the user inputs through the spaces between each word, but I do not know the best approach to achieve that. I know how to do substring with spaces, but I do not know hot to get the amount of words to initialize the array neither how to create the loop to get all words.

Being like:

String search = "Luis Costa";

String[] words;

words[0] = "Luis"

words[1] = "Costa"

Luis G. Lino
  • 163
  • 12
  • 5
    `string.Split(' ')`? – CodeCaster May 04 '16 at 14:22
  • Sounds like your best bet here is to create an index using the values you want searchable and then search on that, this is quite a big topic for 1 question. – War May 04 '16 at 14:23
  • 1
    Check this out: http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation – Dylan Slabbinck May 04 '16 at 14:24
  • 1
    @Darth_Wardy Actually the question seems to boil down to how to split a string. Everything else is just details. – juharr May 04 '16 at 14:25
  • I have had this before it's a complex search situation where a single term can scan a row for keywords. Nothing here answers the OP's question and the marked duplicate is actually not even related – War May 04 '16 at 14:35
  • @LuisGLino https://gist.github.com/TehWardy/202f6524db5e9887fc57954e44a2da2e – War May 04 '16 at 14:37
  • @Darth_Wardy this question is only about obtaining a string array where the search terms are separated by whitespace, which the duplicate answers perfectly. For how to query multiple columns on multiple values, that's an entirely different question (that also has been answered many times before). – CodeCaster May 04 '16 at 14:44

1 Answers1

2

You can split your string based on a space and store it in array like this:

string search = "Luis Costa";
string[] words = search.Split(' ');
Jesse Williams
  • 653
  • 7
  • 21
Mostafiz
  • 7,243
  • 3
  • 28
  • 42