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.

Si8
  • 9,141
  • 22
  • 109
  • 221

1 Answers1

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();
Community
  • 1
  • 1
Jhon
  • 582
  • 7
  • 28
  • I am auto generating the columns so that won't work unfortunately :/ – Si8 Jan 06 '16 at 20:50
  • I just noticed that, I am searching if I can set the encoding to false programatically – Jhon Jan 06 '16 at 20:51
  • I see what you are doing... Setting the boundfield settings before assigning to gridview. I will test it out. Thanks. – Si8 Jan 07 '16 at 01:48
  • I had to manipulate and add it in the `RowDataBound` function. Thank you for the help. – Si8 Jan 07 '16 at 14:08