Solved

Visual Studio 2013 / Windows Form

Posted on 2015-01-16
17
191 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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
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
Comment Utility
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
Comment Utility
(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
Comment Utility
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
Comment Utility
I have just noticed that you have dropped the %'s around the {1}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:hhnetworks
Comment Utility
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
Comment Utility
I don't think that you need the % around Name
0
 

Author Comment

by:hhnetworks
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
Comment Utility
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
Comment Utility
Dan, THANK YOU!  It worked perfectly.

ChloesDad, I appreciate all your help as well.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now