如何在C#中过滤DataGridView中的数据?
我正在构建一个windows窗体应用程序。在这个应用程序中,我想根据组合框使用过滤器。我尝试了一些代码,但它给了我一个异常,无效列“饮料”。喝酒是我的一个品类。我怎样才能解决这个问题?我对表单中的代码应该在哪里使用感到困惑。组合框方法或页面加载方法如何在C#中过滤DataGridView中的数据?,c#,winforms,filter,datagridview,C#,Winforms,Filter,Datagridview,我正在构建一个windows窗体应用程序。在这个应用程序中,我想根据组合框使用过滤器。我尝试了一些代码,但它给了我一个异常,无效列“饮料”。喝酒是我的一个品类。我怎样才能解决这个问题?我对表单中的代码应该在哪里使用感到困惑。组合框方法或页面加载方法 private void populateCat() { try { Con.Open(); string CatValue = CatCb2.SelectedValue.ToString();
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);
}
}
首先转到组合框的事件,然后在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();
问题:
“饮料”列无效
错误是在将“drink”值传递到查询时发生的
string query = $"select * from ProductTbl where ProdCat={CatValue};";
通过上面的语句,您的SQL查询将成为
select * from ProductTbl where ProdCat=drink
因此,您将得到以下错误:
“饮料”列无效
假设您的SQL查询必须是:
select * from ProductTbl where ProdCat='drink'
建议
数据集
。似乎您只是来自1个表的查询结果。因此,您应该使用DataTable
SqlConnection
、SqlCommand
和SqlDataAdapter
应用“使用块/声明”,以便在进程结束时处理资源和连接
注意:此解决方案供帖子所有者了解根本原因以及如何修复。要过滤DataGridView而不需要从数据库中获取,请参考@OlivierRogier关于过滤DataGridView的建议。这是否回答了您的问题?&&您应该使用参数化查询。感谢您的帮助@Olivier Rogier。但我仍然对此感到困惑。@ImdadMiran关于什么?例如,在控件文本更改事件处理程序(或为combobox选择Indexchanged)上,您可以将以下内容放入BindingSource中:
var ds=DataGridView.DataSource;如果(EditFilter.Text!=“”){string filter=DefaultFilter.IsNullOrEmpty()?“”:$“{DefaultFilter}和“*{EditFilter.Text}*”;}之类的“ds.filter=$”{filter}消息;否则ds.filter=DefaultFilter;DataGridView.ClearSelection()代码>。其中DefaultFilter是一个实例变量,消息是DB列名。您确定DB字段名是饮料?也许喝点什么?您需要我将此作为详细答案发布吗?谢谢@OliverRogier,我发现了我的问题。我在一个方法中这样做,并在您推荐的位置使用了该方法,但引发了一个异常。@Miad您可能需要在查询中添加一些引号(或参数更好)以使此答案正确
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);
}
}