Hello, Today i will share code which provide facility for search data in grid view and how to highlight the searched result. Instead of buying the third party controls this code provide easy way to do this.
You just need to make some CSS and user defined function call to make it working check out below code.
here we can make CSS class which contain color code to highlight , two user define functions for the search text and for bind high light CSS class. And on onrowcreate we can implement CSS of Onmouseover & Onmouseout for highlight row on which we have cursor. and gridview operation is also covered using sqldatasource.
we just need to set some CSS ans JS call (very simple) and some code behind.
We are call two events of grid view to make this
1. OnRowCreated
2. EditRowStyle-BackColor
3. user define function
How we can done this
1. Create CSS code for the row hover & highlight search result
Two user define function for the high light and set/remove CSS class first function defines how to highlight text by string functions ( not to worry for understand just put it in your code) and second to replaces words.
CSS
2. Code behind : for RowCreated event
protected void gridSample_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("onmouseover", "this.className='OnOver'"); e.Row.Attributes.Add("onmouseout", "this.className='OnOut'"); } }
Two user define function for the high light and set/remove CSS class first function defines how to highlight text by string functions ( not to worry for understand just put it in your code) and second to replaces words.
User Define Function
public string HighlightText(string InputTxt)
{
string Search_Str = txt_catname.Text;
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
}
public string ReplaceKeyWords(Match m)
{
return ("" + m.Value + "");
}