Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

Searching in the GridView Control

There was a post on ASP.NET forums where the user asked how to search inside the GridView control. I was managed to implement this feature although if you plan to use the following code then I would highly recommend that you refactor it as it lacks in performance. In my opinion since you are searching inside the GridView the whole procedure should be handled on the client side using JavaScript or VBScript. Anyhow the following code uses the server side button click event to parse the GridView and highlight the search fields.

 

protected void Button1_Click(object sender, EventArgs e)

{

string searchString = txtSearch.Text;

StringBuilder sb = new StringBuilder();

DataTable dt = new DataTable();

List<GridViewRow> rows = new List<GridViewRow>();

 

foreach (GridViewRow row in GridView1.Rows)

{

TableCellCollection cells = row.Cells;

foreach (TableCell cell in cells)

{

if (cell.Text.ToLower().StartsWith(searchString.ToLower()))

{

cell.BackColor = System.Drawing.Color.Yellow;

foreach (TableCell c in cells)

{

sb.Append(c.Text);

}

sb.Append("<BR>");

}

else

{

cell.BackColor = System.Drawing.Color.White;

}

}

}

Label1.BackColor = System.Drawing.Color.Yellow;

Label1.Text = sb.ToString();

}

 

The code above simply goes through the GridView row by row and cell by cell. And if it finds a string that starts with the searchString then it highlights it and append it in the StringBuilder object.

Published Monday, October 02, 2006 4:40 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Searching in the GridView Control @ Sunday, October 08, 2006 6:54 AM

hi...thanks for your good article .but I wanna know if you could tell me the VB code of this ? if you can send it to my mail: peril_man2006@yahoo.com thanku

ali

# re: Searching in the GridView Control @ Sunday, October 08, 2006 3:16 PM

Hi Ali,

I suggest that you check out free code convertors. Check out the one developed by Kamal Patel it is pretty nice.

azamsharp

# re: Searching in the GridView Control @ Tuesday, October 10, 2006 7:06 AM

Thank u for your reply.I wanna say that I did it but it but it had an error: A potentially dangerous Request.Form value was detected from the client (txtCSharp="...); List rows =..."). -I tried to come to c# programming but it also had error for List rows =..."). -I delete the line of "List rows = new List()" and in VB it works. I have another question:if or gridview's paging is enabled how could we go thhrogh pages.I mean if the searched item is in the second page how can we go to that page and make the row noticeable(yellow) for user?

Ali

# someProblems @ Tuesday, October 10, 2006 7:07 AM

Thank u for your reply.I wanna say that I did it but it but it had an error: A potentially dangerous Request.Form value was detected from the client (txtCSharp="...); List rows =..."). -I tried to come to c# programming but it also had error for List rows =..."). -I delete the line of "List rows = new List()" and in VB it works. I have another question:if or gridview's paging is enabled how could we go thhrogh pages.I mean if the searched item is in the second page how can we go to that page and make the row noticeable(yellow) for user?

Ali

# re: Searching in the GridView Control @ Wednesday, October 11, 2006 2:31 AM

You can turn off the paging when you click the search button will enable the whole GridView to appear. Then you can search in the whole GridView find the searched item and display it in the Label or Literal control and finally turn on the paging again.

azamsharp

# Good but not enough @ Friday, October 13, 2006 10:23 AM

hi again sorry for disturb. I did your solution and it was useful.but do you know any sources for working with gridview paging? because for this example I don't want to turn off paging because I want to use it for many records and if I turn off the paging,speed will decrease. 2.another question that I have is if I want to make the search for One filed (for example first filed) and this field is sorted ,is there a way to encrypt search process to this filed?and again if the found item is in the second page is there a way to show the related page without turn the paging off? thank u friend!

Ali

# Good but not enough @ Saturday, November 04, 2006 1:49 AM

hi again sorry for disturb. I did your solution and it was useful.but do you know any sources for working with gridview paging? because for this example I don't want to turn off paging because I want to use it for many records and if I turn off the paging,speed will decrease. 2.another question that I have is if I want to make the search for One filed (for example first filed) and this field is sorted ,is there a way to encrypt search process to this filed?and again if the found item is in the second page is there a way to show the related page without turn the paging off? thank u friend! Ali

peril_man

# re: Searching in the GridView Control @ Thursday, November 09, 2006 5:32 PM

Hi Azam, I had a requirement long ago to search specific value inside the columns in a gridview. My code is very similar to yours. However, if the gridview has pagination enabled in it and if the user enters certain value in the search text box and searches for it in gridview, it is not able to span through the pages and show the results. It always look for the value in the current page that the gridview is displaying. Do you have any solution for this? Let me know if this is not making sense. Thank you for sharing your knowledge with users like me.

Anu

# Searching in GridView With Paging Enabled @ Thursday, November 09, 2006 5:55 PM

Some time back I blogged about searching in the GridView control ( http://aspadvice.com/blogs/azamsharp/archive/2006/10/02/Searching-in-the-GridView-Control.aspx

AzamSharp

# re: Searching in the GridView Control @ Thursday, November 09, 2006 5:57 PM

Check out the updated post using the link below:

http://aspadvice.com/blogs/azamsharp/archive/2006/11/09/Searching-in-GridView-With-Paging-Enabled.aspx

azamsharp

# re: Searching in the GridView Control @ Thursday, July 12, 2007 5:37 AM

Hi the updated article is very nice and useful. thanks a lot for posting this. I would like to ask you for your help on searching. I need to search by the text that i have typed in text box and it should search description column in database table. if it finds any of the word (not only starts with), respective records shold be populated in gridview. could you please help me in this regard. please send solution to my id: neetusirena@gmail.com

Neetu

# re: Searching in the GridView Control @ Monday, August 27, 2007 3:48 AM

it is so easy to understand

Pamela

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below