Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview that is populated on page load. I added a search option and when user clicks button I want the matching row to be highlighted in gridview. The grid will display all the data but will only highlight the matching row. Below is my code but it doesn't highlight the matching row. Any suggestions?

What I have tried:

My Gridview:
<asp:GridView ID="Search" runat="server" CssClass="table table-bordered table-hover" AutoGenerateColumns="False">
                         <Columns>
                             <asp:BoundField DataField="ID" Visible="False" />
                             <asp:BoundField DataField="ITEM No#" HeaderText="ITEM No" ItemStyle-CssClass="ITEM No" />
                             <asp:BoundField DataField="Discipline Required" HeaderText="Discipline Required" />
                             <asp:BoundField DataField="Service Description" HeaderText="Service Description" />
                             <asp:BoundField DataField="Institution" HeaderText="Institution" />
                             <asp:BoundField DataField="Award Date" HeaderText="Award Date" />
                             <asp:TemplateField HeaderText="Update Record">
                                                       <ItemTemplate>
                                                           <asp:Button ID="RadioButton1" Text="Update" ForeColor="Blue" runat="server" OnClick="Update_Click" CommandName="Update" />
                                                       </ItemTemplate>
                                                       </asp:TemplateField>
                         </Columns>
                     </asp:GridView>


Highlight search code:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           e.Row.Cells[0].Text = Regex.Replace(e.Row.Cells[0].Text, RefNo.Text.Trim(), delegate (Match match)
           {
               return string.Format("<span style = 'background-color:#D9EDF7'>{0}</span>", match.Value);
           }, RegexOptions.IgnoreCase);
       }
   }
Posted
Updated 14-Mar-17 1:20am

1 solution

refer this

<form id="form1" runat="server">
        <asp:TextBox ID="RefNo" runat="server"></asp:TextBox>
        <asp:Button ID="btn" runat="server"  Text="search" OnClick="btn_Click"/>
        <asp:GridView ID="Search" runat="server" CssClass="table table-bordered table-hover" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
            <Columns>
                <asp:BoundField DataField="ID" Visible="False" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Address" HeaderText="Address" />
            </Columns>
        </asp:GridView>

    </form>




protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        BindData();
    }
}

public void BindData() {
    DataTable dt = new DataTable();
    dt.Columns.Add("ID");
    dt.Columns.Add("Name");
    dt.Columns.Add("Address");
    dt.Rows.Add(1, "karthik", "Bangalore india");
    dt.Rows.Add(2, "prasanth", "Tamilnadu india");
    dt.Rows.Add(2, "sachin", "Mumbai india");
    dt.Rows.Add(2, "sehwag ", "delhi india");

    Search.DataSource = dt;
    Search.DataBind();
}


protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string seachtext = RefNo.Text.Trim();
        int[] colsToCheck = new int[] {0,1,2};  // columns to be searched [ zero based index ]
        for (int i = 0; i < colsToCheck.Length; i++)
        {
            int colIndex = colsToCheck[i];
            e.Row.Cells[colIndex].Text = Regex.Replace(e.Row.Cells[colIndex].Text, seachtext, delegate(Match match)
            {
                return string.Format("<span style = 'background-color:yellow'>{0}</span>", match.Value);
            }, RegexOptions.IgnoreCase);
        }

    }
}

protected void btn_Click(object sender, EventArgs e)
{
    BindData();
}
 
Share this answer
 
Comments
Nomfundo Cindi 14-Mar-17 8:25am    
I have a created database already!
Karthik_Mahalingam 14-Mar-17 8:26am    
What is the issue
Nomfundo Cindi 14-Mar-17 8:26am    
{ SqlDataAdapter dadapter;
DataSet dset;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myContractorsConn"].ConnectionString);
string sql = "select [Discipline Required], [Service Description], Institution from PSP_Report ORDER BY [Discipline Required], [Service Description], Institution ASC";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
Nomfundo Cindi 14-Mar-17 8:29am    
it doesn't highlight the matching row, when i run it and it doesn't display any errors
Nomfundo Cindi 14-Mar-17 8:42am    
Thank so much your OnRowDataBound code worked!! where do i rate you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900