Add CSS to the Grid Record

Asked

Viewed 79 times

3

I’m trying to add a bold in a record on the grid, but it appears as "<b>Nome</b>" and not "Name" as it should.

Exemplo

I’m doing it for code behind, through a LINQ

Follow my method displayUltimoSorteio():

private void exibirUltimoSorteio()
{
    var apostadores = (from j in _contextEntities.TB_JOGOS
                 join s in _contextEntities.TB_SORTEIOS on j.SORT_ID equals s.SORT_ID
                 join a in _contextEntities.TB_APOSTADORES on j.APO_ID equals a.APO_ID
                 select a).Distinct().OrderBy(x => x.APO_NOME).ToList();

    var tb_apostador = new List<TB_APOSTADORES>();

    tb_apostador.AddRange(apostadores.Select(apostador => new TB_APOSTADORES{
        APO_NOME = "<b>"+apostador.APO_NOME+"</b>"
    }));

    gvResultados.DataSource = tb_apostador;
    gvResultados.DataBind();
}

2 answers

5


In the event Rowdatabound from your grid, you can use:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[0].Text);
  e.Row.Cells[0].Text = decodedText;
}

5

You can use this row to color your column (not counting the title):

gvResultados.Columns[0].ItemStyle.Font.Bold = true;

The interesting thing is that it is possible to apply any style you want, just change the part .ItemStyle.Font.Bold by the formatting you need.

I also think you can use this for (which allows more control when changing styles, by row and by column ):

for (int i = 0; i < gvResultados.Rows.Count; i++)
{
    gvResultados.Rows[i].Cells[0].Font.Bold = true;
}

In this loop, Gridview is traversed by rows (Rows), so if you want to paint the cells in the first column, use Cells[0], if it’s the second column, use Cells[1] and so on.

Note: I’m not sure if column titles (APO_NOME) count as Row, if it counts, use i = 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.