filter datagridview based on textbox value (exact match)

ravi kumar 331 Reputation points
2020-12-17T04:49:06.993+00:00

Hello all ,

In my winform application , i need to filter my datagridview based on my text box text , but as i am using like keyword for matching " the values "Accepted" & "Accepted on deviation" are considered as same , hence if i type accepted both of these values are populating(but if i type "Accepted on deviation" it is getting filtered as desired), i tried changing my query but every time i am getting syntax error , kindly help me how to over come this.

i need to filter only columns with "Accepted" when i type "Accepted" instead of getting both the values "Accepted" & "Accepted on deviation"

here is my code:

private void Btnentryview_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                Refreshdata();
                return;
            }
            string strkeyword = textBox2.Text.Trim().ToString();

            StringBuilder sb = new StringBuilder();
            if (textBox2.Text == "Accepted")
            {
                sb.AppendFormat("(Convert(SpoolID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
                sb.AppendFormat("OR (Status LIKE '*" + "{0}" + "*')", strkeyword);
            }
            if (textBox2.Text == "Accepted On Deviation")
            {
                sb.AppendFormat("(Convert(SpoolID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
                sb.AppendFormat("OR (Status LIKE '*" + "{0}" + "*')", strkeyword);
            }
            if (textBox2.Text == "Rejected")
            {
                sb.AppendFormat("(Convert(SpoolID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
                sb.AppendFormat("OR (Status LIKE '*" + "{0}" + "*')", strkeyword);
            }
            if (textBox2.Text == "Rework")
            {
                sb.AppendFormat("(Convert(SpoolID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
                sb.AppendFormat("OR (Status LIKE '*" + "{0}" + "*')", strkeyword);
            }
            if (textBox2.Text == "All")
            {
                Refreshdata();
            }
            string strFilter = sb.ToString();
            iP_SpoolsBindingSource.Filter = strFilter;

            if (iP_SpoolsBindingSource.Count != 0)
            {
                iP_SpoolsDataGridView.DataSource = iP_SpoolsBindingSource;
            }
            else
            {
                MessageBox.Show("No Records Found", "Search Result", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                Refreshdata();
                return;
            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,320 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
2,412 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,950 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,646 Reputation points
    2020-12-17T07:15:58.343+00:00

    If you mean the Status column, then try replacing

    sb.AppendFormat("OR (Status LIKE '*" + "{0}" + "*')", strkeyword);
    

    with

    sb.AppendFormat("OR Status LIKE '{0}'", strkeyword);
    

    or with

    sb.AppendFormat("OR Status = '{0}'", strkeyword);
    

    Otherwise give details about the column to be filtered and strkeyword.


0 additional answers

Sort by: Most helpful