【问题标题】:How can I search string value from database and show in DataGridView?如何从数据库中搜索字符串值并在 DataGridView 中显示?
【发布时间】:2013-09-03 11:45:12
【问题描述】:

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

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

这是我的代码:

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

【问题讨论】:

  • 您的过滤器将类似于Product ID = SomeText,这是不正确的。您需要像 Product ID = 'SomeText' 一样使用它

标签: c# winforms datagridview bindingsource


【解决方案1】:

您刚刚错过了过滤器值前后的'

[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()

【讨论】:

    猜你喜欢
    • 2015-08-12
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多