0

Here is my code:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //**************************************
        int aa = textBox1.Text.Length;

        var qry = (from p in dc.Products
                   where p.ProductName.Substring(aa) == textBox1.Text.Trim()
                   select p).ToList();
        productDataGridView.DataSource = qry;
    }

When I enter a letter in the textbox the datagrid become empty

Sami-L
  • 5,553
  • 18
  • 59
  • 87
  • Use `p.ProductName.Contains(textBox1.Text.Trim())` instead of `p.ProductName.Substring(aa) == textBox1.Text.Trim()` – Chandu Jul 27 '12 at 14:01
  • @Chandu p.ProductName.Contains(textBox1.Text.Trim()) mean that the result should contain the letter entered in the textbox at any position in it, what I need is that the items returned by the query should have the text in textbox as first lest characteres – Sami-L Jul 27 '12 at 14:10

2 Answers2

2
private void textBox1_TextChanged(object sender, EventArgs e)
    {
        var searchValue = textBox1.Text.Trim();//you can add a ToUpper();
        var qry = (from p in dc.Products
                   where p.ProductName.StartsWith(searchValue);//you can add a ToUpper() to p.ProductName
                   select p).ToList();
        productDataGridView.DataSource = qry;
    }
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

never and I say never use == to compare 2 strings always use equals() or contains() if you are comparing 2 strings most likely u will get a false eventhough the 2 strings look exactly the same. The difference is that each of the string point to a different object. Basically everytime u make a string it creates a new object of the class String.

Anthony
  • 427
  • 2
  • 6
  • Could you please give an example with equals() using my initial question, I have tried it and datagrid is returned empty when filling textbox. – Sami-L Jul 27 '12 at 14:18
  • This is only true in java. http://stackoverflow.com/questions/1659097/c-string-equals-vs – Fr33dan Jul 27 '12 at 14:21
  • well I switched shorty to c# from java. I have never tested this theory in c#. the link proves your point I stand corrected ;) – Anthony Jul 27 '12 at 14:52