473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search button to filter a DataGrid


Hi all,

I hope someone can help with this relatively simple problem.
I am building a timesheet application using ASP.NET C# with Visual
Studio 2003.As it is only a protoype application, my database has been
made in MSDE.

I have a 'View Resources' page, the purpose of which is to show all
employees, taken from that table in the database. This works fine, up
to a point. I have a DataGrid, which is done with a DataSet and a
DataAdapter. I now want the user to be able to 'filter' the DataGrid,
using a text box for input, and a 'search' button. I have tried
different ways of doing this but can't get it to work. I imagine that
it is achieved by running some kind of different SQL statement (with a
% LIKE % operator) in the btnSearch_Click method, and re-binding the
Data Grid after the filter has finished.

So for example the user could enter 'John' in the 'First Name' text box
and click 'search', and the Data Grid would refresh accordingly with
all records LIKE 'John'.

My Page_Load code looks like this currently
-------------------------------------------------------------------------
this.sqlDataAda pter1.Fill(this .dsResources1);
DataGrid1.DataB ind();
sqlConnection2. Close();

this.DataGrid1. PageIndexChange d += new
System.Web.UI.W ebControls.Data GridPageChanged EventHandler(th is.GridPageInde xChanged);
--------------------------------------------------------------------------------------------

Hope someone can help, I'm sure the btnSearch_Click method is where I
need to put the code.

Thanks in advance,

Al

Mar 5 '06 #1
9 2739
DataSet11.Emplo yees.DefaultVie w.RowFilter = " Firstname like '%" &
TextBox1.Text & "%' "
--
Terry Burns
http://TrainingOn.net
"thebison" <al************ @btinternet.com > wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .

Hi all,

I hope someone can help with this relatively simple problem.
I am building a timesheet application using ASP.NET C# with Visual
Studio 2003.As it is only a protoype application, my database has been
made in MSDE.

I have a 'View Resources' page, the purpose of which is to show all
employees, taken from that table in the database. This works fine, up
to a point. I have a DataGrid, which is done with a DataSet and a
DataAdapter. I now want the user to be able to 'filter' the DataGrid,
using a text box for input, and a 'search' button. I have tried
different ways of doing this but can't get it to work. I imagine that
it is achieved by running some kind of different SQL statement (with a
% LIKE % operator) in the btnSearch_Click method, and re-binding the
Data Grid after the filter has finished.

So for example the user could enter 'John' in the 'First Name' text box
and click 'search', and the Data Grid would refresh accordingly with
all records LIKE 'John'.

My Page_Load code looks like this currently
-------------------------------------------------------------------------
this.sqlDataAda pter1.Fill(this .dsResources1);
DataGrid1.DataB ind();
sqlConnection2. Close();

this.DataGrid1. PageIndexChange d += new
System.Web.UI.W ebControls.Data GridPageChanged EventHandler(th is.GridPageInde xChanged);
--------------------------------------------------------------------------------------------

Hope someone can help, I'm sure the btnSearch_Click method is where I
need to put the code.

Thanks in advance,

Al

Mar 5 '06 #2
Hi Terry,
Thanks for your quick reply. I have tried that code, with some
variations but still cannot quite get the search button to have any
effect on my Data Grid when pressed? In the code below, I have
simplified the requirement to simply return all records with a DeptID
less than 2. Unfortunately it still returns all records in the table?
(Note I am running the Search on the 'Dept' table now, not 'Resource')
-------------------------------------------------------------------
private void btnSearch_Click (object sender, System.EventArg s e)
{
DataView dv = dsDept1.Tables[0].DefaultView;
dv.RowFilter = "DeptID <2";
DataGrid1.DataS ource = dv;
DataGrid1.DataB ind();
}
----------------------------------------------------------------------------

Any suggestions? I'm sure I am close to getting it to work! Is it
something to do with the fact I have another DataGrid1.DataB ind(); in
my Page_Load section of the code?

Thanks again!

Mar 6 '06 #3
Al,

Try this... place a DataView on your page designer, set the Table to
the appropriate table in your DataSet and use the DataView as the
Grid's datasource (you need to add the tables to your DataSet, if you
haven't already).

Then you can use this code like this in your Click event:

newDataView.Row Filter = "DeptID <2";
DataGrid1.DataB ind();

I didn't look into the problem you're having, but I suspect the problem
is related to binding to the DataSet in the PageLoad and changing it to
the DataView in the click method. I think you'll find a DataView
created at design makes RowFilters and sorting expressions easier to
use anyway.

Hope this helps,
Carl

Mar 6 '06 #4
Carl,

That works just right! Thanks!

Would you know how I can manipulate the RowFilter to return the rows
based on a user-input textbox?

I've tried something like

dataView1.RowFi lter = "DeptName LIKE '%'+txtDeptName .text+'%'";

But this does not work.

Any suggestions?

Thanks for your help

Al

Mar 7 '06 #5
Extra info - the error message for the code above is

Exception Details: System.Data.Eva luateException: Cannot find column
[txtDeptName.tex t].

This occurs when I click on the 'Search' Button

Thanks!

Mar 7 '06 #6
I showed you how to do this, in my first post, which by the way is a live
working example.

--
Terry Burns
http://TrainingOn.net
"thebison" <al************ @btinternet.com > wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...
Extra info - the error message for the code above is

Exception Details: System.Data.Eva luateException: Cannot find column
[txtDeptName.tex t].

This occurs when I click on the 'Search' Button

Thanks!

Mar 7 '06 #7
Terry,

Sorry, I confused myself with this! I tried the code you posted, and it
does exactly what I wanted! Thanks a lot!

The next step for me is to apply this principle to make a search on
another form, holding lists of Time Periods with Start and End Dates.
With this search, I need to to possibly search by values from TWO user
text boxes. So in theory the user could search for values between Start
Date 11/01/05 and End Date 30/01/05 OR just search based on ONE of the
two input text boxes. So if the textbox is blank the search will just
ignore it. Does that make sense?

I've googled it, but can't quite find what I'm after

Any help much appreciated.

Al

Mar 7 '06 #8
I think you accidently wrapped txtDeptName.Tex t in double quotes, so
the literal value is being passed rather than the Text property. Try
this:

dataView1.RowFi lter = "DeptName LIKE '%'" + txtDeptName.tex t + "'%'";

or...

dataView1.RowFi lter = string.Format(" DeptName LIKE '%{0}%'",
txtDeptName.tex t);

Glad this worked for you,
Carl

Mar 7 '06 #9
Yes, the SQL syntax is

SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2

I have not tried using this in a table, so I would be interested to see if
it works.

= "column_nam e BETWEEN 01/01/2005 AND 01/01/2006"

let me know

--
Terry Burns
http://TrainingOn.net
"thebison" <al************ @btinternet.com > wrote in message
news:11******** **************@ z34g2000cwc.goo glegroups.com.. .
Terry,

Sorry, I confused myself with this! I tried the code you posted, and it
does exactly what I wanted! Thanks a lot!

The next step for me is to apply this principle to make a search on
another form, holding lists of Time Periods with Start and End Dates.
With this search, I need to to possibly search by values from TWO user
text boxes. So in theory the user could search for values between Start
Date 11/01/05 and End Date 30/01/05 OR just search based on ONE of the
two input text boxes. So if the textbox is blank the search will just
ignore it. Does that make sense?

I've googled it, but can't quite find what I'm after

Any help much appreciated.

Al

Mar 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2093
by: N. Graves | last post by:
Hi, I want to have a Search Dialog box that has several text box and fields to build a search and display the results in a form. I can do everything that I need to if I us a report but I would like to have the search from data displayed in a form. The structure that I have for this was take from the Asset Manger from MS. Anyway I open...
9
20276
by: Christopher Koh | last post by:
I will make a form which will search the database (just like google interface) that will look/match for the exact name in the records of a given fieldname. Any suggestions on how to make the code?
8
3199
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
4
2435
by: Luis Ferrao | last post by:
Comming from Windows Forms development, I never had problems with this issue before. What i'm trying to achieve is an iTunes like incremental search. For those who never used iTunes, it's basically a text field with a key stroke event that filters the data grid as the users types in a string. The more text in the field, the fewer fields...
2
7241
by: Homey! | last post by:
Hello all I am new to Access. I have imported data from an old FoxPro 2.x database. This is probably the most basic function but I cant get a search box to work. I need to search for company name and cant figure it out in access. Tony (homey)
0
2066
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently...
1
1857
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Dear All I need some help. I've got a deadline for this friday and i'm stuck. what i'm trying to do is have 1 textbox, datagrid and 1 button then when the user enter a search string, and press the button, I want the result to be displayed in the datagrid. At the moment, when the user press the button the system checks if there is any...
3
2199
by: Elainie | last post by:
I would like to search a form with many fields on it, with out using the search facility through access. Througth a drop down list if possible.... How would I go about this? How could I also create this from a top bar menu option too...? Elaine
0
2711
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only, cannot serch by combine 3 table) Example I have the query table below, how do I make the code to seach based on the query from this: SELECT...
0
7484
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7440
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1902
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 we have to send another system
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.