Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Visual Studio 2013 / Windows Form

Posted on 2015-01-16
17
Medium Priority
?
226 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

927 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