4

I have a textbox for search (that is, textBox1) A user, for example, enters "aba" in textBox1. "abandon" puts in datagridiew1. The user clicks on datagriview1:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    richTextBox_MWE.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    if ("richTextBox_MWE.Text like '%" + textBox1.Text + "%'")
    {
        label5.BackColor = Color.Green;
    }
}

I want if "abandon" is such as "aba" in textBox1, label5.BackColor becomes Green.

1
  • 1
    Generally, you'd like to consider Regular Expressions (msdn.microsoft.com/en-us/library/hs600312.aspx) as a .NET equivalent for SQL LIKE operator, but in this particular case you can just use .Contains() method. Commented Jun 20, 2014 at 12:37

4 Answers 4

4

Simple way is use the textBox1(where actually filter content going to change) change event

if(!String.IsNullOrEmpty(richTextBox_MWE.Text) && richTextBox_MWE.Text.Trim().Contains(textBox1.Text.Trim()))
{
  label5.BackColor = Color.Green;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot. I also want customed Contains Function, that is, If I enter "1" just Contains Function findes "1" not "11" or "21" or ....
2

You want to use some kind of mix of C# and sql :) You can use the String.Contains method to achieve what you want.

if(richTextBox_MWE.Text != null
    && richTextBox_MWE.Text.Contains(textBox1.Text.Trim())
{
    ...
}

Comments

0
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    richTextBox_MWE.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
    if (!String.IsNullOrEmpty(richTextBox_MWE.Text) && !String.IsNullOrEmpty(textBox1.Text) && richTextBox_MWE.Text.Contains(textBox1.Text.Trim()))
    {
        label5.BackColor = Color.Green;
    }
}

Here the contains will accept the value to be searched.

1 Comment

'' like '%%' is true.
0

See snippet from my code. The txtProductCode is a text box that user is filling the product_code for searching in the list view.

        string tmpProductCode = txtProductCode.Text.Trim();
        string tmpProductCodePattern = "^" + Regex.Escape(tmpProductCode).Replace("%", ".*") + "$";

In my loop of product_code(s) the prodCode will contain the product_code value for each loop.

        productCodeClause = false;              
        if (tmpProductCode.Equals(""))
        {
            productCodeClause = true;
        }
        else
        {
            if (Regex.IsMatch(prodCode, tmpProductCodePattern))
            {
                productCodeClause = true;
            }
        }

I hope this will be helpful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.