【问题标题】:How to filter data in DataGridView in C#?如何在 C# 中过滤 DataGridView 中的数据?
【发布时间】:2021-08-01 12:39:12
【问题描述】:

我正在构建一个 Windows 窗体应用程序。在这个应用程序中,我想根据组合框使用过滤器。我尝试了一些代码,但它给了我一个例外,Invalid Column 'drink'。饮料是我的类别之一。我怎么解决这个问题?我对应该在哪里使用表单中的代码感到困惑。组合框方法或页面加载方法

private void populateCat()
{
    try
    {
        Con.Open();
        string CatValue = CatCb2.SelectedValue.ToString();
        string query = $"select * from ProductTbl where ProdCat={CatValue};";
        SqlDataAdapter sda = new SqlDataAdapter(query, Con);
        SqlCommandBuilder builder = new SqlCommandBuilder(sda);
        var ds = new DataSet();
        sda.Fill(ds);
        ProdDGV.DataSource = ds.Tables[0];
        Con.Close();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

【问题讨论】:

  • 你应该使用参数化查询。
  • 感谢您的帮助@Olivier Rogier。但我仍然对此感到困惑。
  • @ImdadMiran 关于什么?例如,在控制文本更改事件处理程序(或组合框的 selecteindexchanged)上,您可以输入如下内容:var ds = DataGridView.DataSource as BindingSource; if ( EditFilter.Text != "" ) { string filter = DefaultFilter.IsNullOrEmpty() ? "" : $"{DefaultFilter} AND"; ds.Filter = $"{filter} Message LIKE '*{EditFilter.Text}*'"; } else ds.Filter = DefaultFilter; DataGridView.ClearSelection();。其中 DefaultFilter 是一个实例变量,Message 是数据库列名。您确定数据库字段名称是 drink 吗?也许Drink?您需要我将其发布为详细答案吗?
  • 谢谢@OliverRogier 我发现了我的问题。

标签: c# winforms filter datagridview


【解决方案1】:

问题:

“饮料”列无效

错误是当您将“drink”值传递给查询时。

string query = $"select * from ProductTbl where ProdCat={CatValue};";

通过上面的语句,你的SQL查询会变成

select * from ProductTbl where ProdCat=drink

因此你会得到这个错误:

“饮料”列无效

假设您的 SQL 查询必须是:

select * from ProductTbl where ProdCat='drink'

建议

  1. 使用参数化查询将值添加到参数中并防止SQL Injection attack。

  2. 您不需要DataSet。似乎您只是来自 1 个表的查询结果。因此你应该使用DataTable。

  3. 建议对SqlConnection、SqlCommand 和SqlDataAdapter 应用“使用块/声明”以在进程结束时释放资源和连接。

private void populateCat()
{
    try
    {
        using (SqlConnection Con = new SqlConnection(/* Connection string */))
        {
            Con.Open();

            using (SqlCommand command = new SqlCommand("select * from ProductTbl where ProdCat= @ProdCat", Con))
            {
                string catValue = CatCb2.SelectedValue.ToString();
                command.Parameters.Add("@ProdCat", SqlDbType.NVarchar).Value = catValue;

                using (SqlDataAdapter sda = new SqlDataAdapter(command))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    ProdDGV.DataSource = dt;
                }
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

注意:此解决方案是为了让帖子所有者了解根本原因以及如何解决它。过滤DataGridView而不需要从数据库中获取,可以参考@OlivierRogier关于过滤DataGridView的建议。

【讨论】:

    【解决方案2】:

    首先转到组合框的事件,然后在 selectindexChange 中双击

     Con.Open();
            string CatValue = CatCb2.SelectedValue.ToString();
            string query = $"select * from ProductTbl where ProdCat={CatValue};";
            SqlDataAdapter sda = new SqlDataAdapter(query, Con);
            SqlCommandBuilder builder = new SqlCommandBuilder(sda);
            var ds = new DataSet();
            sda.Fill(ds);
            ProdDGV.DataSource = ds.Tables[0];
            Con.Close();
    

    【讨论】:

    • 我在一个方法中做到了这一点,并在你推荐的地方使用了该方法,但抛出了一个异常。
    • @Miad 您可能需要在查询中添加一些引号(或参数最好)以使此答案正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2015-01-21
    相关资源
    最近更新 更多