Hi zuzia,
zuzia
I need to find a keyword in gridview (on data bind) and apply bold, and background color to the cell in a row. How would I accomplish that?
According to your description, you may want to highlight these column contains your keywords when binding the data, the following code does that.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HiddenField weekdayname = new HiddenField(); weekdayname = (HiddenField)e.Row.FindControl("HiddenField1"); for (int i = 0; i < e.Row.Cells.Count; i++) { if (e.Row.Cells[i].Text.Contains("yourkeywords")) e.Row.Cells[i].BackColor = System.Drawing.Color.Blue; } } }
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"><Columns><asp:BoundField HeaderText="username" DataField="Name" ItemStyle-Width="200" HeaderStyle-Width="220" FooterStyle-Width="220"/><asp:BoundField HeaderText="address" DataField="Address" /></Columns></asp:GridView>
Besides, if you use template to bind your data in your gridview, you could use FindControl method to find the text for the cell.
e.Row.Cells[i].FindControl("youcontrolid") or e.Row.Cells[i].FindControl("youcontrolid")
Best Regards,
Klein zhang