Search code examples
c#winformsdatagridviewbindingsource

How can I search string value from database and show in DataGridView?


I want to search value from database and delete value from databaseshow. I can Search and but when I click the delete button its give me the following Error:

Syntex error:missing operand after '='.

Here is my code:

private void sID_textBox7_TextChanged_1(object sender, EventArgs e)
        {
            try
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = dataGridView4.DataSource;
                bs.Filter = "[Product ID]=" + sID_textBox7.Text.ToString();
                dataGridView1.DataSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Solution

  • You have just missed the ' before and after the filter value.

    [Product ID] = SomeText should be [Product ID] = 'SomeText'

    private void sID_textBox7_TextChanged_1(object sender, EventArgs e)
        {
            try
            {
                BindingSource bs = new BindingSource();
                bs.DataSource = dataGridView4.DataSource;
                bs.Filter = "[Product ID]=" + "'" + sID_textBox7.Text + "'";
                dataGridView1.DataSource = bs;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    

    See BindingSource.Filter Property

    EDIT

    Writing sID_textBox7.Text.ToString() has no sens. .Text property returns a String, no need to use .ToString().