问题描述
我有四列:projectName,cartridgeType,cartridgeCode和registeredDate。
在combobox1中的projectName它有六个项目的六个projectName's。
cartridgeType也是combobox2和它有大约15项。
cartridgeCode文本框。
我现在得到的只是一个条件过滤。例如,如果用户将选择projectName,它将显示注册到此projectName的所有记录,其中包含dataGridView中的所有不同的cartridgeType。
所以我想根据多个条件制作一个过滤器,例如,如果用户选择一个projectName,它将给出我上面写的结果,然后在此结果中搜索特定的cartridgeCode或cartridgeType。
结果必须是,在dataGridView中只显示一种类型的projectName,一个cartrdigeType和一种类型的cartridgeCode。
如果你能帮助我,我将不胜感激。
i have a four columns: projectName,cartridgeType, cartridgeCode and registeredDate.
projectName in combobox1 and it has six item's-six projectName's.
cartridgeType also combobox2 and it has around 15 items.
cartridgeCode textbox.
all what i got now is filtering by one condition. for example if user will choose projectName it will show all records which is registered to this projectName with all different cartridgeType's in dataGridView.
so i want to make a filter by multiple criteria for example if user will choose a projectName it will give a result as i wrote above and then in this result to search specific cartridgeCode or cartridgeType.
The result must be, in dataGridView to show just a one type of projectName,one cartrdigeType and with one type of cartridgeCode.
I will be grateful if u can help me with this.
try
{
DataView dvAbc = new DataView(sTable);
dvAbc.RowFilter = string.Format("projectName LIKE '%{0}%'", comboBox1.Text);
dataGridView4.DataSource = dvAbc;
//this code for combobox1 to filter data by projectName
}
catch { }
推荐答案
sTable = GetQueryData(strQuery); //
try
{
DataView dvddd1z = new DataView(sTable);
dvddd1z.RowFilter = string.Format("cartridgeCode LIKE '%{0}' AND projectName LIKE '%{1}'", textBox14.Text + (textBox14.Text == string.empty?String.Empty : "%", comboBox1.Text + (comboBox1.Text == string.empty)?String.Empty : "%"); // These are two parameters, see below for explanation
dataGridView4.DataSource = dvddd1z;
}
catch { }
}
假设您的文本框保存Part1的值,组合保存Project1
现在,你的代码正在过滤磁带就像Part1Project1(我希望你没有:) :)你也要求项目名称中包含Part1Project1的项目也不对。
使用上面的代码,您将搜索包含名称part1的cartrige和包含Project1的项目
而不是使用if()else要添加过滤器,只要有新条件,就需要额外的ifs,使用三元运算符(表达式?真值:虚假值)
此外,谷歌有点关于SQL注入...考虑如果你的一个用户放了'; GO; DROP TABLE会发生什么项目;' -
进入你的文本框:)
Let's say your textbox holds the value of Part1, combo holds Project1
Right now, your code is filtering for records where cartridge is like Part1Project1 (which I hope you don't have :) ) and also you're asking for the project where project name contains Part1Project1 which also isn't right.
With the code above, you'll search for the cartrige which contains name part1 and the project that contains Project1
Instead of using if () else to add filters which would require additional ifs wherever you have new condition, use ternary operator (expression ? true value : false value)
Also, google a bit about SQL injection...consider what would happen if one of your users put "'; GO; DROP TABLE PROJECTS; '--"
into your textbox :)
这篇关于如何在C#Winform应用程序中使用多个条件筛选数据?我使用Mysql Server作为数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!