Problems with special characters in ASP.NET GRIDVIEW

3

I'm building a dynamic GRIDVIEW, where the information for each TD will be a DIV.

exemplo: <td><div id="d1" class="redips-drag t1">z</div></td>

I'm filling in the variant as follows:

if (dtWorkServiceTimeByWorkerAndDate.Rows.Count > 0)
    finalResult = "<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\">" + formatSecondsInHourAndMinute(Convert.ToInt32(dtWorkServiceTimeByWorkerAndDate.Rows[0]["work_ServiceTime"])) + "</div>";

My problem is that GRID does not recognize the div as an object, but as a String. The value of each column is coming out as follows:

exemplo &lt;div id=&quot;column1&quot; class=&quot;redips-drag t1&quot;&gt;23:59&lt;/div&gt;

Does anyone know how I can do this to avoid this problem?

Vlw.

EDIT

To build the grid, I'm using a function that returns a DataTable, as follows:

public DataTable constructWorkerAvailabilityTime()
    {
        DateTime fDateDataType = new DateTime(Int32.Parse(fYear), Int32.Parse(fMonth), Int32.Parse(fDay));
        DateTime tDateDataType = new DateTime(Int32.Parse(tYear), Int32.Parse(tMonth), Int32.Parse(tDay));

        this.fromDate = fDateDataType.ToString("yyyy-MM-dd");
        this.toDate = tDateDataType.ToString("yyyy-MM-dd");

        DataTable dtFinalResult = new DataTable();
        List<String> allWorkers = getAllWorkers();
        List<String> columnsSequence = getDateColumnsSequence();

        dtFinalResult.Columns.Add("worker", typeof(string));

        int count = 0;

        foreach (string column in columnsSequence)
            dtFinalResult.Columns.Add(column, typeof(string));

        foreach (string worker in allWorkers)
        {
            DataRow newRow = dtFinalResult.NewRow();

            newRow["worker"] = getWorkerName(worker);

            foreach (string column in columnsSequence)
            {
                HtmlString htmlString = getWorkServiceTimeByWorkerAndDate(worker, column, count);
                newRow[column] = htmlString;
                //newRow[column] = "@Html.Raw(" + getWorkServiceTimeByWorkerAndDate(worker, column, count) + ")";
                count++;
            }

            dtFinalResult.Rows.Add(newRow);
        }

        return dtFinalResult;
    }

private HtmlString getWorkServiceTimeByWorkerAndDate(string worker, string executionDate, int count)
    {
        HtmlString finalResult = new HtmlString("");
        DataTable dtWorkServiceTimeByWorker = cmd.selectWorkServiceTimeByWorker(worker);
        DataTable dtWorkServiceTimeByWorkerAndDate = cmd.selectWorkServiceTimeByWorkerAndDate(worker, executionDate);

        if (dtWorkServiceTimeByWorker.Rows.Count == 1)
            finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">" + formatSecondsInHourAndMinute(Convert.ToInt32("28860")) + "</div>");
        else if (dtWorkServiceTimeByWorker.Rows.Count > 1)
        {
            if (dtWorkServiceTimeByWorkerAndDate.Rows.Count > 0)
                finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">" + formatSecondsInHourAndMinute(Convert.ToInt32(dtWorkServiceTimeByWorkerAndDate.Rows[0]["work_ServiceTime"])) + "</div>");
            else
                finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">00:00</div>");
        }

        return finalResult;
    }

And finally I fill the grid in the Load page:

protected void Page_Load(object sender, EventArgs e)
    {
        WorkerManagement wm = new WorkerManagement();
        GridWorkerAvailability.DataSource = wm.constructWorkerAvailabilityTime();
        GridWorkerAvailability.DataBind();
        GridWorkerAvailability.SelectedIndex = -1;
    }
    
asked by anonymous 20.08.2015 / 13:06

2 answers

1

To solve the problem, I used:

    protected void GridWorkerAvailability_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int cellCount = e.Row.Cells.Count;

        for (int j = 1; j < cellCount; j++)
        {
            string encoded = e.Row.Cells[j].Text;
            e.Row.Cells[j].Text = Context.Server.HtmlDecode(encoded);
        }
    }
    
21.08.2015 / 14:23
0

You can use Html.Raw() .

Example:

@Html.Raw(finalResult)
    
20.08.2015 / 14:56