Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visual Studio 2013 / Windows Form

Posted on 2015-01-16
17
Medium Priority
?
221 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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