如何从数据库中搜索字符串值并在DataGridView中显示?

时间:2013-09-03 11:45:12

标签: c# winforms datagridview bindingsource

我想从数据库中搜索值并从databaseshow中删除值。我可以搜索,但是当我点击删除按钮时,它会给我以下错误:

  

Syntex错误:'='后缺少操作数。

这是我的代码:

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);
            }
        }

1 个答案:

答案 0 :(得分:0)

您在过滤器值之前和之后错过了'

[Product ID] = SomeText应为[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);
        }
    }

请参阅BindingSource.Filter Property

修改

sID_textBox7.Text.ToString()没有感觉。 .Text属性返回String,无需使用.ToString()