Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
 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 { }
Posted
Updated 13-Nov-14 2:09am
v6
Comments
Sinisa Hajnal 13-Nov-14 8:41am    
Why not just add AND cartridgeType LIKE xy AND AND AND...?
Rustam777 13-Nov-14 23:31pm    
if (comboBox1.Text != "")
{

try
{
DataView dvddd1z = new DataView(sTable);
dvddd1z.RowFilter = string.Format("cartridgeCode LIKE '%{0}%' AND projectName LIKE '%{0}%'", textBox14.Text+comboBox1.Text);
dataGridView4.DataSource = dvddd1z;
}
catch { }
}
else {
try
{
DataView dvddd1 = new DataView(sTable);
dvddd1.RowFilter = string.Format("cartridgeCode LIKE '%{0}%'", textBox14.Text);
dataGridView4.DataSource = dvddd1;
}
catch { }

it didnt help
Sinisa Hajnal 14-Nov-14 2:06am    
How it didn't help? You have to be more specific. Also, you're putting both variables txtBox14 and combobox1 into same like parameter. Is that what you need you have projectName in one field and cartridge in another?

1 solution

First, don't use empty catch blocks. If you have them, use them. If there is no exception handling, why have them?

Second, here is the code for filtering.

C#
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 { }
}


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 :)
 
Share this answer
 
Comments
Rustam777 14-Nov-14 22:09pm    
Thank u Sinisa Hajal all my mistake was that i wrote for both condition 0 in curly braces:
dvddd1z.RowFilter = string.Format("cartridgeCode LIKE '%{0}%' AND projectName LIKE '%{0}%'", textBox14.Text+comboBox1.Text);
in the second condition i have to increase it to one. i did it:
dvddd1z.RowFilter = string.Format("cartridgeCode LIKE '%{0}%' AND projectName LIKE '%{1}%'", textBox14.Text+comboBox1.Text);
and it worked :)
Sinisa Hajnal 17-Nov-14 2:04am    
You're still having + instead if , between textBox14.Text and comboBox1.Text...check your RowFilter final value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900