Solved

Visual Studio 2013 / Windows Form

Posted on 2015-01-16
17
196 Views
Last Modified: 2015-01-18
I am new to Visual Studio and Windows Forms.

Im trying to filter a DataGridView by both an unbound ComboBox (cb_searchtype) that selects a column and a Textbox (tb_keywords) that selects keywords within that column.

The goal is that the dropdown has three entries matching column headers (unbound), and the Keywords textbox will select text from within the matching column. Im using the code below, but it fails with a syntax error:
----
 private void searchclick(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = "'%" + cb_SearchType.Text + "%' + like '%" + tb_keywords.Text + "%'";
            dataGridView1.DataSource = bs;
        }
----

Im trying to get the dropdown (cb_searchtype) to pass the Text value of the column header so that the value passed by tb_keywords will Select records.  If I statically set the column name with [column] in the above code, it works fine for the keywords, but I need to pass the value of the dropdown to that filter.

The logic is "SELECT * FROM MyTable WHERE cb_searchtype = [dropdown value] AND tb_keywords = [keywords value]"

Thank you in advance.
0
Comment
Question by:hhnetworks
  • 8
  • 7
  • 2
17 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 40554745
Are you debugging this and examining the value of cb_SearchType.Text?

You probably want the cb_SearchType.SelectedText value.

You're modifying the search based on the value of the selected item, right? You'd want to index off the selected item and not just the combo box text itself.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40554907
Also, although not a syntax error use & rather than + to concatenate strings, or use the string.format function

 bs.Filter = string.format("'%{0}%' + like '%{1}%'", cb_SearchType.Text ,tb_keywords.Text)

Open in new window

0
 

Author Comment

by:hhnetworks
ID: 40555259
Yes Dan, I want to query the column called by the value of the ComboBox and filter it by the text called in the TextBox.

To ChloesDad: I incorporated your code but get a syntax error as shown in the screenshot:

Debug window
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555306
I notice that I have left a rogue + in the text, should be

 bs.Filter = string.format("'%{0}%' like '%{1}%'", cb_SearchType.Text ,tb_keywords.Text)

Open in new window


This may be the problem
0
 

Author Comment

by:hhnetworks
ID: 40555307
Update:  after playing around with it, I seem to have resolved the error, however everything I select returns zero rows...
 
private void searchclick(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();       
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = string.Format("'{0}' Like '{1}'", cb_searchtype.SelectedText, tb_keywords.Text);
            dataGridView1.DataSource = bs;
        }

Open in new window

0
 

Author Comment

by:hhnetworks
ID: 40555315
(Sorry, I hadnt seen your previous comment)  I replaced my code with yours and dont get the syntax error, but also get no rows returned.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555325
If you put a breakpoint in after the filter line, does the string.format give you what you expect
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555335
I have just noticed that you have dropped the %'s around the {1}
0
 

Author Comment

by:hhnetworks
ID: 40555453
It appears to be returning values correctly. In the example screenshot below, "Name" is the column name I want, and "Trailer" is a keyword within many rows of that column. Just cant figure out why it wont return rows. (I did put the %s back after your last post)1-17-2015-2-44-16-PM.png
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555510
I don't think that you need the % around Name
0
 

Author Comment

by:hhnetworks
ID: 40555518
Deleted the %s around Name, but no rows returned.  Is there a better way to pass the variables of the Combo and Tex boxes...maybe in a SQL statement?  Like I said Im about 4 days into my experience in C#, so Im as green as a cucumber.
0
 

Author Comment

by:hhnetworks
ID: 40555529
Here's my whole code...Im thinking since the DGV populates OnLoad, am I missing anything to "repopulate" on the the filtered results?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStoreManager
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
        }

        private void OnShowABoutBox(object sender, EventArgs e)
        {
          
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
           

            this.taSearchTableAdapter.Fill(this.oNLINE_STOREDataSet.taSearch);

        }

        private void SearchTypeChanged(object sender, EventArgs e)
        {

        }

        private void KeywordChanged(object sender, EventArgs e)
        {

        }

        private void searchclick(object sender, EventArgs e)
        {
            BindingSource bs = new BindingSource();       
            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = string.Format("'{0}' like '%{1}%'", cb_searchtype.Text, tb_keywords.Text);
            dataGridView1.DataSource = bs;
        }
             

        private void label1_Click(object sender, EventArgs e)
        {

        }
       




    }
}

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555536
I have been assuming that the datagrid is populated with the entire database initially, and then you are filtering it down. If the datagrid is initially empty then you need to ensure that the datasource is set correctly
0
 

Author Comment

by:hhnetworks
ID: 40555551
The datagrid is populating when I run the form...and yes my intention is to filter those results down using the two variables...In the bs.Filter statement, If I statically assign a column name it works fine....

Im thinking its just not building a query or filter the right way.

I appreciate your help so far
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 40555586
Although this example doesn't say so

http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter(v=vs.110).aspx

Try adding

DataGridView1.Update after the filter is applied
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 40556279
Try this:

bs.Filter = string.Format("{0} like '%{1}%'", cb_searchtype.Text, tb_keywords.Text);

Open in new window


You don't want your column name in single quotes. For example:

WHERE MyColumn LIKE '%Hello Kitty%'
Not
WHERE 'MyColumn' LIKE '%Hello Kitty%'
0
 

Author Closing Comment

by:hhnetworks
ID: 40556289
Dan, THANK YOU!  It worked perfectly.

ChloesDad, I appreciate all your help as well.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question