1

I have the following C# function:

public string CheckInCollection(long lngProvId)
{
    //...
    foreach (var item in collList)
    {
        strColl += item.Id.ToString() + " (" + item.Title + ")" + "\r\n";
    }

    return strColl;
}

The following C# function:

public void PopulateGridView(bool blType)
{
    if (blType == false)
    {
    }
    else
    {
        strCollFinalized = "" + ddlContent.SelectedItem + "|" + CheckInCollection(Convert.ToInt64(ddlContent.SelectedItem.Value)) + "";
        string[] strL = strCollFinalized.Split('|');

        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("Provider");
        DataColumn dc1 = new DataColumn("Collection");

        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);

        DataRow dr = dt.NewRow();
        dr[dc] = strL[0];
        dr[dc1] = strL[1];

        dt.Rows.Add(dr);
        gvData.DataSource = dt;
        gvData.DataBind();
    }
}

When the gridview is generated the second column is displayed in one line but when I check the source, it is displaying in the next line.

How can I modify so that the returned string shows each strColl in a new line.

4
  • try adding "<br/>" instead of "\r\n" Commented Jan 6, 2016 at 20:38
  • Then the column shows the <br /> instead of translating into Html. Commented Jan 6, 2016 at 20:40
  • Take a look at this: codeverge.com/asp.net.presentation-controls/… Commented Jan 6, 2016 at 20:43
  • try adding a Literal or Label control and add text to it Commented Jan 6, 2016 at 20:44

1 Answer 1

6

As @user2169261 suggested, you can add <br> instead of \r\n

Then you can set the HtmlEncode property of the column you want to show the newline to false.

For example:

<Columns>
    <asp:BoundField DataField="Whatever" HtmlEncode="False" />
</Columns>

For autogenerated columns, you can try following approach (as per this answer)

Make your own inspection of the DataTable and create an explicit BoundColumn for each column:

foreach (DataColumn column in dt.Columns)
{
    GridViewColumn boundColumn = new BoundColumn
    {
        DataSource = column.ColumnName,
        HeaderText = column.ColumnName,
        HtmlEncode = false
    };
    gvData.Columns.Add(boundColumn);
}

gvData.DataSource = dt;
gvData.DataBind();
Sign up to request clarification or add additional context in comments.

4 Comments

I am auto generating the columns so that won't work unfortunately :/
I just noticed that, I am searching if I can set the encoding to false programatically
I see what you are doing... Setting the boundfield settings before assigning to gridview. I will test it out. Thanks.
I had to manipulate and add it in the RowDataBound function. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.