473,473 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using a Textbox to filter from DataGridView and SQLite Database.

80 New Member
Hey all,
I'm trying to get some more user friendly things in my program done. Now I'm trying to filter by typing in a text box and it filters to what you are typing and shows the entire row.

This is what I currently have:
Expand|Select|Wrap|Line Numbers
  1. private void txtFilter_TextChanged(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 SQLiteConnection connect = new SQLiteConnection(connString);
  6.                 connect.Open();
  7.                 string whereQuery = "select Channel from Test where Channel '" + txtFilter.Text + "'";
  8.                 SQLiteDataAdapter adap = new SQLiteDataAdapter(whereQuery, connString);
  9.  
  10.                 DataSet ds2 = new DataSet();
  11.                 adap.Fill(ds2);
  12.                 dataGridView1.DataSource = ds2.Tables[0];
  13.             }
  14.             catch (Exception ex)
  15.             {
  16.                 MessageBox.Show("An error occured.\n" + ex.ToString(),"Error!");
  17.             }
  18.         }
I get an exception error though:
Expand|Select|Wrap|Line Numbers
  1. SQLite error
  2. near "'N'": syntax error
// N is what I started typing to start the filter.

I don't exactly get what I've done wrong.
Any help would be much appreciated. Thanks


EDIT:
Ok, so I've edited the code now to this:
Expand|Select|Wrap|Line Numbers
  1. private void txtFilter_TextChanged(object sender, EventArgs e) // FILTER TODO
  2.         {
  3.                 DataView view = new DataView();
  4.                 SQLiteConnection connect = new SQLiteConnection(connString);
  5.                 connect.Open();
  6.                 view.RowFilter = "Channel like '%" + txtFilter.Text + "%'";
  7.         }
No more exception errors, just not what I want it to do. It just keeps everything there. Any ideas?
Mar 11 '13 #1
13 16487
Mikkeee
94 New Member
I wouldn't open your db every time a keypress has been made due to the overhead involved. Instead I would either use a select statement on your data or set your View rowfilter. Your updated code unnecessarily creates a view, opens a connection, and set's the row filter but it's never assigned to your DataGridView. You should be fine with one line of code in there instead:
Expand|Select|Wrap|Line Numbers
  1. dataGridView1.RowFilter = "Channel like '%" + txtFilter.Text + "%'";
  2.  
Mar 11 '13 #2
M1kkelZU
80 New Member
When I use that line of code, it can't find RowFilter. The rror message basically says that DataGridView doesn't contain a definition for RowFilter.
so I changed it a little bit
Expand|Select|Wrap|Line Numbers
  1. DataView view = new DataView();
  2.             dataGridView1.DataSource = view;
  3.             view.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
Now it searches but does not show the record with a channel name that starts with the letter I inserted in to my textbox.
Mar 11 '13 #3
Mikkeee
94 New Member
I was a little quick on the trigger....
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = (DataTable)DataGridView1.DataSource;
  2. dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
  3.  
Mar 11 '13 #4
M1kkelZU
80 New Member
Using that gives me this error":
Expand|Select|Wrap|Line Numbers
  1. Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.
on this Line:
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = (DataTable)dataGridView1.DataSource;
I have no clue what it means lol.
Mar 11 '13 #5
Mikkeee
94 New Member
How are you setting your DataSource? In your code above you set it to a datatable. I ran this same filter on my database without issue.
Mar 11 '13 #6
M1kkelZU
80 New Member
Maybe I need to use dTable instead of making a new datatable.

As I've declared dTable first, I'll try that.

EDIT:
after doing that, it gives me this error:
Cannot find column [Channel].

The code:
Expand|Select|Wrap|Line Numbers
  1.             dataGridView1.DataSource = dTable;
  2.             dTable.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
Mar 11 '13 #7
Mikkeee
94 New Member
Use this in your form load:
Expand|Select|Wrap|Line Numbers
  1. SQLiteConnection connect = new SQLiteConnection(connString);
  2. connect.Open();
  3. SQLiteDataAdapter adap = new SQLiteDataAdapter("select Channel from Test", connect);
  4.  
  5. DataSet ds = new DataSet();
  6. adap.Fill(ds);
  7. dataGridView1.DataSource = ds.Tables[0];
  8.  
And this in your TextChanged event:
Expand|Select|Wrap|Line Numbers
  1. DataTable dt = (DataTable)DataGridView1.DataSource;
  2. dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
  3.  
Mar 11 '13 #8
M1kkelZU
80 New Member
If I do that, I get the same error that it can't cast the object type Bindingsource to type DataTable. It should work though :L

EDIT:
Did it just a tad differently:
Expand|Select|Wrap|Line Numbers
  1.  private void txtFilterChannel_TextChanged(object sender, EventArgs e)
  2.         {
  3.             SQLiteConnection connect = new SQLiteConnection(connString);
  4.             connect.Open();
  5.             SQLiteDataAdapter adap = new SQLiteDataAdapter("select Channel from Test", connect);
  6.  
  7.             DataSet ds = new DataSet();
  8.             adap.Fill(ds);
  9.             dataGridView1.DataSource = ds.Tables[0];
  10.  
  11.             DataTable dt = (DataTable)dataGridView1.DataSource;
  12.             dt.DefaultView.RowFilter = "Channel like '%" + txtFilterChannel.Text + "%'";
  13.         }
Now, it only shows the name of the channel, not like the date or recording date. I have the feeling we've sorted it by column and not by row?
Mar 11 '13 #9
Mikkeee
94 New Member
You're query string is only selecting the Channel field. Just change it to "select * from Test" if you want all the fields.
Mar 11 '13 #10
M1kkelZU
80 New Member
Ah ok thanks, I really should be a bit more logical in these situations. thanks man :)
Mar 11 '13 #11
henimex
2 New Member
i have a similar issue but i would like to choose column via combobox

Expand|Select|Wrap|Line Numbers
  1.  dt.DefaultView.RowFilter = "product_name LIKE '%" + textBox4.Text + "%'";
that works fine but i want to use combobox1.text not product_name.
i have tried some knows formats but doesnt worked example

Expand|Select|Wrap|Line Numbers
  1. dt.DefaultView.RowFilter = "'"+comboBox1.Text+"' LIKE '%" + textBox4.Text + "%'";
need help on this thank you

compalte code is here (i have tried some other ways to do it but rowfilter just worked fine)

Expand|Select|Wrap|Line Numbers
  1.  set_connection();
  2.                 con.Open();
  3.                 com = con.CreateCommand();
  4.                 //string comtext = "SELECT * FROM products WHERE product_name LIKE'" + textBox4.Text + "'";
  5.                 //string comtext = "SELECT * FROM products WHERE product_name LIKE @pname";
  6.                 //com.Parameters.AddWithValue("@pname", "%Kar%");
  7.                 string comtext = "SELECT * FROM urunler";
  8.                 adapt = new SQLiteDataAdapter(comtext, con);
  9.                 ds.Reset();
  10.                 adapt.Fill(ds);
  11.                 dt = ds.Tables[0];
  12.                 dataGridView1.DataSource = dt;
  13.                 dt.DefaultView.RowFilter = "product_name LIKE '%" + textBox4.Text + "%'";
  14.                 //dt.DefaultView.RowFilter = "'"+comboBox1.Text+"' LIKE '%" + textBox4.Text + "%'";
  15.                 con.Close();
May 8 '19 #12
Mikkeee
94 New Member
The single quotes you have around your column name is the issue. Try this:

Expand|Select|Wrap|Line Numbers
  1. dt.DefaultView.RowFilter = "[" + comboBox1.Text + "] LIKE '%" + textBox4.Text + "%'";
May 8 '19 #13
henimex
2 New Member
@Mikkeee
thank you so much! i was really stucked before [] thank you
May 8 '19 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dcrespo | last post by:
Hi all... Does anybody know about the best SQLite Database Manager open source web-based or standalone app under windows? I tried: - SQLiteManager (web-based -...
3
by: Dan V. | last post by:
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select. I read at : microsoft.public.dotnet.framework.adonet "As others have posted: There is no...
5
by: Steve S | last post by:
Heres what I want to do...User types into a texbox, clicks a button, the button saves that text to a file. The problem is that when I click the submit button, any changes made to the textbox are...
0
by: Tulasi | last post by:
Hello, any one help me.I am new in SQLite database.My problem is that ,how SQLite Datebase is connected in Vb.Net?.Please Send me the source code because idont know the Connectionstring of SQLite...
3
by: Danny Nielsen | last post by:
Hi I am having trouble figuring out how to use combobox in a datagridview. What seemed to be a simple task turned out not to arrghh. I have a dataGridView with 4 columns, one of them is column...
1
by: martin1 | last post by:
Hi, All, DataGridView is populated with all coumns from database since i need some columns data for condition statement, after that I want to show rest of column in the DataGridView, so how to...
1
by: dillipdhal | last post by:
Hi This is Dillip from HSBC Software,Hyd. Now I m working in a financial domain. Please help me in "How can I insert data from a datagridview to database in vb .net and also retrive the data "
1
by: MFayaz | last post by:
Hello! I have to refresh datagridview when database update , IS there any way to solution that my datagridview auto refresh when any user update database. Thanks in Advance
2
by: Zabivb | last post by:
how to use mkdir to create dynamically folders using textbox ? coding ====== <?php $foldername="text"; mkdir = $foldername; ?>
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.