Solved

Visual Studio 2013 / Windows Form

Posted on 2015-01-16
17
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

632 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