4

I have this line of code which uses a dataview view_1 and I'm trying to filter the datagridview by product_name and its size using the RowFilter. Here is the code:

view_1.RowFilter = "product_name = '" + cboProduct.Text + "' AND size = " + cboSize.Text + "";

And when I try to run the application it says Missing operand after '=' operator. So what is that missing operand?

Harvey
  • 399
  • 4
  • 15
  • 31

4 Answers4

8

You have a missing white-space at 'AND

So replace

'AND 

with

' AND 

Is size a string-column or an int-column? If it's a string you need quotation marks around too:

AND size = '" + cboSize.Text + "'";

or even better, use String.Format as others have commented since it insreases readability:

view_1.RowFilter = string.Format("product_name = '{0}' AND size = '{1}'"
            , cboProduct.Text
            , cboSize.Text);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @Harvey: Is size a string-column or an int-column? If it's a string you need quotation marks around too. `AND size = '" + cboSize.Text + "'";`. Btw, instead of rewriting your question to remove the wrong part you should have added instead the information that it's still not working. Now my answer makes no sense anymore but it could have been helpful for others with a similar problem. – Tim Schmelter May 21 '13 at 07:20
  • Okay I get it..after putting a white-space I tried to put it in the else clause and now it's working. Thank you. – Harvey May 21 '13 at 07:32
  • @Harvey: Also edited my answer to show you how you can use `String.Format` to increase readability. – Tim Schmelter May 21 '13 at 07:34
4

Write like this

view_1.RowFilter = "product_name = '" + cboProduct.Text + "' AND size = " + cboSize.Text + "";

Missing White space problem

Edit

You can also use string.Format

view_1.RowFilter =  String.Format("product_name = '{0}' AND size = {1}", cboProduct.Text,cboSize.Text);  
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83
1

I don't know what is wrong I tried to filter first if the text of cboProduct and cboSize is empty or if no selection has been made and now it's working. Thank you. Here is the code

if (cboProduct.Text == string.Empty || cboProduct.SelectedIndex == -1 || cboSize.Text == string.Empty || cboSize.SelectedIndex == -1)
            {
                view_1.RowFilter = string.Empty;
            }
            else
            {
                view_1.RowFilter = "product_name = '" + cboProduct.Text + "' AND size = " + cboSize.Text + "";
            }
Harvey
  • 399
  • 4
  • 15
  • 31
0

If the variable product_name is having a value that contains a space then the value needs to be wrapped in square brackets in the filter expression: So,just wrap the variable product_name by pair of square brackets like this [product_name]

view_1.RowFilter = "[product_name] = '" + cboProduct.Text + "' AND size = " + 
cboSize.Text + "";
Ankit_K
  • 96
  • 1
  • 7