开发者

Colored Data in 1 String in a gridview in asp.net?

I have a r开发者_开发技巧esultgridview from a search query.

In this gridview I have a text e.g. "This is a nice Car for you"

If the user search for the word "Car" I like to show the word "Car" in the string in the gridview in red and bold.

How to do it?


You should define a css-class f.e.:

.searchmatch
{
   color: red;
   font-weight:bold;
}

... and in GridView.RowDataBound you could split the name into the parts that match and the parts that do not match. In the TemplateField of the Gridview-Column with the name you could f.e. add a Div(runat="server"). In RowDataBound you can find it with its id, its from Type HtmlGenericControl. Then you can use the InnerHtml property to assign the match part's span its searchmatch-css-class.

UPDATE: I think this could be helpful in GridView too: Highlighting Search Keywords in a DataGrid Web Control


As I still do not really understand what you are trying to achieve this is just an (not so) educated guess :)

Maybe you could use the GridView.RowCreated Event event to set a CSS loaded <span class="..."> tag around the search text (that comes from a text box or whatever)?

EDIT

RowDataBound (as suggested by others) would probably be the better event to do what you want.


foreach (GridViewRow gr in GridView1.Rows)
        {
            for (int i = 2; i <= 32; i++)
            {
                if (gr.Cells[i].Text like '%"car"%'")
                {
                   gr.Cells[i].ForeColor = System.Drawing.Color.FromName("White");
                   gr.Cells[i].BackColor = System.Drawing.Color.FromName("#275F8F");
                }
                else if (gr.Cells[i].Text == "L")
                {
                    gr.Cells[i].ForeColor = System.Drawing.Color.FromName("White");
                    gr.Cells[i].BackColor = System.Drawing.Color.FromName("Red");
                }
                else if (gr.Cells[i].Text == "P")
                {
                    gr.Cells[i].ForeColor = System.Drawing.Color.FromName("Black");

                }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜