user955566
user955566

Reputation: 39

c#: Search the DataGridView columns text both in Upper and Lower cases

I want to search the DataGridView columns text both in Upper and Lower cases, which is being input through text box. I used the following code which search the string but only if matching the exact case. I need that text string case should be either upper or lower

        string searchstring = textBox1.Text;
        try
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (searchstring == "")
                    row.Selected = false;
                else
                {
                   if (row.Cells[1].Value.ToString().Contains(searchstring))
                   {
                        this.dataGridView1.MultiSelect = true;
                        row.Selected = true;
                        int indx = row.Index;
                    }
                    else
                        row.Selected = false;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Upvotes: 0

Views: 107

Answers (1)

Brandon
Brandon

Reputation: 3276

If I'm understanding correctly, you are trying to match strings without worrying about case. Instead of using Contains(), try using String.Equals() and pass in the StringComparison.OrdinalIgnoreCase parameter. That is what ignores case.

Here is a good link on string comparison.

string val = row.Cells[1].Value.ToString();
if (String.Equals(val, searchstring, StringComparison.OrdinalIgnoreCase))
{
    // do work
}

Upvotes: 2

Related Questions