Interpret br / in a gridView cell

1

I have to separate by marks of \n the content of a cell of GridView to get different lines to appear in a cell.

The code in which I find myself is the following:

Modifying the DataTable :

cmd = new OracleCommand();
cmd.Connection = cnn;
cmd.CommandText = comando;
da.SelectCommand = cmd;
da.Fill(dt);

for (int i = 0; i < dt.Rows.Count; i++)
{
    string text = dt.Rows[i]["OBSFX"].ToString();
    text = text.Replace("\n", " <br /> ");
    dt.Rows[i]["OBSFX"] = text;
}

GridView1.DataSource = dt;
GridView1.DataBind();

Now modifying the GridView directly:

cmd = new OracleCommand();
cmd.Connection = cnn;
cmd.CommandText = comando;
da.SelectCommand = cmd;
da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    string text = GridView1.Rows[i].Cells[16].Text;
    text = text.Replace("\n", "<br/>");
    GridView1.Rows[i].Cells[16].Text = text;
}

GridView1.DataBind();

Neither way works. Any ideas?

    
asked by Geraniego 19.04.2017 в 17:13
source

2 answers

0

Finally I found the answer to my problem. I had to generate an event of RowDataBound and there insert the following code:

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            string encoded = e.Row.Cells[i].Text;  //
            e.Row.Cells[i].Text = encoded.Replace("\n", "<br />");
        }
    }
}

This event is within the GridView and you just have to add the event and the code and it works alone. Thank you very much everyone for your feedback !!! : D I hope someone helps you.

    
answered by 20.04.2017 / 15:57
source
0

The Replace method replaces all occurrences that match the first parameter with the value of the second. In your case, if you want to change <br /> by \n the instruction would be:

text = text.Replace("<br />", "\n");

Just the opposite of how you have it.

Also, for the cell to display more than one line, you may have to adjust a pair of properties in GridView , such as automatic row height and word wrap ( wordwrap ) in the cells .

    
answered by 19.04.2017 в 19:27