-3

Hello i have a datagridview which has datasource of list and this list is:

public class UniqueNounWithFreq
{
    public int freq { get; set; }
    public string word { get; set; }

    public UniqueNounWithFreq(string word, int freq)
    {
        this.freq = freq;
        this.word = word;
    }
}



if (e.KeyChar == (char)13)
{
    foreach (DataGridViewRow item in dataGridView_w2wm2.Rows)
    {
        if (!item.Cells[2].Value.ToString().Contains(textBox1.ToString().ToLower()))
        {
            item.Visible = false;
        }
        else
        {
            item.Visible = true;
        }
    }
}

When I want to hide a row with key press it throws

Row associated with the currency manager's position cannot be made invisible exception

Which you can see here : Unable To set row visible false of a datagridview. I tried the method sugested there but it did not work for me. Also when I check the lengths of the strings I wrote even if they are same they does not match. if you can help me I appreciate that.

vincrichaud
  • 2,218
  • 17
  • 34
Eray
  • 7
  • 2
  • You use `toLower()` only with the second string, is this correct? – Peter Bruins Mar 09 '18 at 13:14
  • 2
    Please read how to create a [mcve]. It's impossible to guess what you are doing here. It's not even clear how `UniqueNounWithFreq` is related to anything – Camilo Terevinto Mar 09 '18 at 13:14
  • 2
    textBox1.ToString() will generate something like "System.Windows.Controls.TextBox: TextBox" - maybe you should be using textBox1.Text. Following on from @PeterBruins comment using .Contains(textBox1.Text, StringComparer.CurrentCultureIgnoreCase) would be better than converting to lower case – PaulF Mar 09 '18 at 13:24
  • @PaulF that's right . you solved my problem. Thank you. – Eray Mar 09 '18 at 13:25
  • I've added that as answer so that it will be highlighted for other users with a similar problem if you care to mark it as correct. – PaulF Mar 09 '18 at 13:27
  • 1
    If you find yourself using `.ToString()` you should question yourself. It's a code smell. There's some places where it's necessary, but it's such an ambiguous and dirty method that you should avoid it when possible. – mason Mar 09 '18 at 13:28
  • If you ever have issue with an equality why didn't you Print/Dump the value as a debug? – Drag and Drop Mar 09 '18 at 13:32
  • `textBox1` is and actual textbox or its content? If it is the former, check PaulF's answer. – Cleptus Mar 09 '18 at 13:34

1 Answers1

3

Using textBox1.ToString() will generate something like "System.Windows.Controls.TextBox: TextBox" - it will create a string of the control type.

You should be using textBox1.Text to get the actual contents of the textbox - it is a string, so does not need converting.

Following on from PeterBruins comment using .Contains(textBox1.Text, StringComparer.CurrentCultureIgnoreCase) would be better than converting to lower case.

You could simplify setting the Visible property without use of an if statement to :

item.Visible = item.Cells[2].Value.ToString().Contains(textBox1.Text,
                             StringComparer.CurrentCultureIgnoreCase);
PaulF
  • 6,673
  • 2
  • 18
  • 29