How to search a gridview with sqldatasource dynamically using multiple search terms from a single textbox control

Could someone please let me know how I can let people search a gridview (with paging and sorting) using a single textbox that accepts multiple search terms? I also want to be able to edit and delete a row from the filtered search results.

I want to be able to apply the search criteria to each column in my gridview.

For example, a user might enter a search string: "jack jones"

Say I have a gridview that has three columns: id, firstname, lastname with the following data:

1 fred flintstone
2 wilma flintstone
3 jack beanstalk
4 jones jack
5 jack jones
6 hungry jack

I want the gridview to be filtered to show rows 3, 4, 5, 6 and respect the edit and delete functions for each row.

At the moment whenever I edit a filtered result, The wrong record is updated. In the above example, editing the first search result "3 jack beanstalk" would actually edit the first row in the table and overwrite its values.

0
Jeremy
2/2/2009 4:20:27 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
1127 Views

Similar Articles

[PageSpeed] 18
Get it on Google Play
Get it on Apple App Store

Hi,

try this one.... for filter and in your search criteria replace all the spaces with "%".

http://forums.asp.net/p/1378651/2908250.aspx

and for Editing, Deleting set the DatakeyNames property of GridView equals to the Primary Key fieldname of the table and then use normal edit template of gridview to edit update the records.... delete will also workfine in the same way.


Mark as answer, if works!!

Subscribe to more articles by sending "Add Me!!" on gordo_matthews0207@yahoo.ca

Thanks and Regards

Alok Arora
0
alok
2/2/2009 6:33:09 AM

do the following things for gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"

 

onrowcancelingedit="GridView1_RowCancelingEdit"

onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"

onrowupdating="GridView1_RowUpdating" >

Make field Id as template field

<Columns>

<asp:TemplateField HeaderText="Id" Visible="False">

<ItemTemplate>

<asp:Label ID="lbId" runat="server" Text='<%# Bind("Id")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="First Name" >

<EditItemTemplate>

<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="lblFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name" >

<EditItemTemplate>

<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="lblLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

</Columns>

then in code behind write the following code for update,delete,edit and cancel

int id; 

protected
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

id =
Int64.Parse((((Label)GridView1.Rows[e.RowIndex].FindControl("lblId")).Text));

string FirstName = (((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text);

string LastName = (((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text);

valResult = TempPrepaidCardManager.Save(objTempPrepaidCard);

write ur your update code here  

GridView1.EditIndex = -1;

GridDataBind();

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

Id = Int64.Parse((((Label)GridView1.Rows[e.RowIndex].FindControl("lblId")).Text));

your delete code here 

GridDataBind();

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

GridDataBind();

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

GridDataBind();

}

GridDataBind is the method where u will be wriring the gridview code.

 

Mark as answer if u find this post useful

0
jijish
2/2/2009 6:53:12 AM

@alok.arora,

 Thanks for your thoughtful response.

I already have a function which filters data based on search criteria, which I posted on the link that you provided:

alok.arora:

I can filter data in multiple columns based on multiple search criteria which is entered in a single text box. However, when using the normal edit template of gridview after applying the filter, the wrong row is updated if the filtered data is in a different order to the original data (as in my example). Delete has the same issue.

0
Jeremy
2/3/2009 12:11:29 AM

@jijish,

Thanks for posting your code.

When working through it, I discovered my problem. My gridview had the EnableViewState property set to false. Changing it to true fixed the problem:

 

<asp:GridView EnableViewState="True">

 Now the correct rows are being updated on a search filtered sqldatasource displayed in a GridView.

0
Jeremy
2/3/2009 1:17:57 AM
Reply:

Similar Artilces:

Gridview Search: use DropDownList and TextBox to search / filter gridview?
Let's say I have a gridview with three columns: First Name, Last Name, ID Number.  Here's an example of what I want to do:I want to display all the records in the gridview to start.  There would be a dropdownlist that lets the user select whether to search by first name, last name, or ID number and a text box that lets them type in what they want to search for (and a button to start the search).  After searching, the gridview would only display the records that matched the criteria.The gridview needs to be editable, sortable, and pageable even after searching/filtering...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

Net::LDAP::Control::Paged unexpected behavior using multiple paged searches against AD [SEC=UNCLASSIFIED]
------=_NextPart_000_003B_01CEEE7E.0BE685F0 Content-Type: multipart/related; boundary="----=_NextPart_001_003C_01CEEE7E.0BE685F0" ------=_NextPart_001_003C_01CEEE7E.0BE685F0 Content-Type: multipart/alternative; boundary="----=_NextPart_002_003D_01CEEE7E.0BE685F0" ------=_NextPart_002_003D_01CEEE7E.0BE685F0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit UNCLASSIFIED Hi . I am finding unexpected behaviour when using this control against our AD servers. I have tried this using 5.8 (perl-ldap 0.4...

Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control
Hi all, I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:   <asp:GridView ID="GridEditSettlement" runat="server" AutoGenerate...

Gridview Search using dropdownlist, textbox, search button.
 I can only search in gridview using a textbox and a search button. What i want to do is. I have a dropdownlist where the items are the columns of the employee table, a textbox where a user input a value. And when the user click the search button it will be displayed on the gridview. something like this. Select * from "Employee" where "dropdownlist_sample" is LIKE "textbox_sample" Here are the columns of my Employee Table EmployeeID, LastName, FirstName, Address, ContactNo, Birthdate, DateHired,  and Empoyee Status. and bt...

Use 1 control validator to multiple textbox control
How can i set my validation control to be used by multiple textboxes. The logic is something like this. <asp:TextBox ID="TextBox1" runat="server" /> <asp:TextBox ID="TextBox2" runat="server" /> <asp:TextBox ID="TextBox3" runat="server" /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Field Required" ValidationGroup="Group1" ControlToValidate="TextBox1,TextBox2,TextBox3" SetFocusOnError="true"></asp:RequiredFieldValidator> I know i can only put 1 value to the ControlToValidate property of "RequiredFieldVal...

How Can I Search Using DropDownList, TextBox, Gridview and a Search Button?
I have a DropDownList with two items:  "Customer" and "Vendor". I like user to select, for example, "Customer", from the DropDownList then in a TextBox, enter the customer name. Clicking the Search button, would return the appropriate data to the GridView-- depending on what has been selected in the DropDownList and what was entered in the TextBox. Is this possible?  Your help would be very much appreciated. Jim   I am trying the same thing, it’s surprising you haven’t received any reply’s by now. Seems like a common task. I am going to try a cross p...

Search query using search term from user input into TextBox
Hi everyone, thank you for the help you provide on this forum, I've learned a lot so far.Right now I'm making a simple Access-Visual Basic 2005 .NET database. I want to make a Search Query that will search the Database for text similar to what the user inputs into TextBox1.Text on the form where the datagrid is displayed.I've created a query that works if:SELECT...FROM...WHERE mycolumn = 'oxygen'for example. It will retrieve and display all matches for oxygen, so the query itself works. But it does not work with the variations I have tried on:SELECT...FROM...WHERE mycol...

Search query using search term from user input into TextBox
Hi everyone, thank you for the help you provide on this forum, I've learned a lot so far.Right now I'm making a simple Access-Visual Basic 2005 .NET database. I want to make a Search Query that will search the Database for text similar to what the user inputs into TextBox1.Text on the form where the datagrid is displayed.I've created a query that works if:SELECT...FROM...WHERE mycolumn = 'oxygen'for example. It will retrieve and display all matches for oxygen, so the query itself works. But it does not work with the variations I have tried on:SELECT...FROM...WHERE mycol...

keyword search not working (GridView controlled by TextBox)
Hi there, I need help here. All I want to do is to search the keyword in the Oracle database. What I used is to add a text box and a GridView. The textbox is to take input and in the GridView, I define to select the entries where the TITLE has the keyword matched. (I used the build-in query function to define the CONTROL to be LIKE. the SQL syntax is can be found in the below code and it was auto generated according to the query defintion) <div> Type a keyword to search <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server"...

Single textbox search on multiple columns with gridview
I need to search multiple columns from a database using a single textbox.So the user can choose to search by firstname, lastname or ID number.If you need me to explain more on what im asking for. Please dont hasitate to hola back !! Hi,You will need to use 3 checkboxes and to write an if statement to check which checkbox is selected. Then to do the search according to the user selection.HTHRegardsASP.NET Hosting (HostingFest) Thanks, Im new to asp.net & c#Any examples that u'v seen out thier to help me in this?Thanks again. Much appreciated................

using controlS to control a gridview
This question is probably posted but i did not bother to look at 10,000+ posts so i am sorry. I've seen the tutorial where he builds a dataset and uses a gridview and controls the gridview with the drop down list box. What i am trying to do is to control the gridview with 2 drop down list boxes. First select the Country you life in and then the State. I've looked at the grid view properties but couldn't find where i can add another parameter. Is it possible to control a grid view (or any form of displaying the information) with more than 1 controller? Thank You.  ...

SqlDataSource Control and Gridview control
Hi, I'm new to web development and learning about these controls.  The books teach you to bind the gridview control to an sqldatasource to perform delete,update,insert functions as well as to list the records in a table.  Is this standard practice for real world applications?  Or is there another way that should be used to display/manage table data in the gridview control? Thanks. Tulsi Using a sqldatasource is the best way I know of for binding data to a gridview.  (I'm pretty new to web development too, so maybe someone else can chime in if they know someth...

Web resources about - How to search a gridview with sqldatasource dynamically using multiple search terms from a single textbox control - asp.net.presentation-controls

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Using The LINQDataSource
-*+There is a new datasource control available in Visual Studio that works in much the same way as the SQLDataSource and the ObjectDataSource. ...

Gabriel Mongeon
Archive - Gabriel Mongeon

moredotnet
M ore d otnet .NET Interview Questions, ASP.NET, C#, VB.NET, XML, SQL Check Out http://www.dotnetUncle.com √ .NET NOTES OOPS in .NET .NET ...

Architectural Overview of the Windows Presentation Foundation Beta 1 Release
Introduction Windows Presentation Foundation Beta 1 Windows Integration New Features WinFX SDK Exploring the New Features Introducing "Express" ...

About Mikesdotnetting
I started this site as somewhere to store answers to bits and pieces that I see asked frequently in the forums at www.asp.net . Rather than type ...

Using MiniProfiler with SqlDataSource ASP.NET WebForms Control
public class ProfiledSqlDataSource : SqlDataSource{ protected override DbProviderFactory GetDbProviderFactory() { // get the "base" DbProviderFactory ...

ASP.NET Pivot Table - RadPivotGrid for ASP.NET AJAX
... reports. RadPivotGrid allows easy customization of all data reports. Try now! Use any declarative data source control (AccessDataSource, SqlDataSource, ...

Sign on
Sign On • Join • Forums Home User Name * Password * Remember me next time ( I forgot my password ) SUBMIT YOUR WTF Content Random Article All ...

ComboBox Sample
ComboBox is an ASP.NET AJAX control that, like the AutoCompleteExtender , combines the flexibility of a TextBox with a list of options that users ...

Resources last updated: 12/12/2015 7:51:35 AM