How to show new line in a GridView column

£可爱£侵袭症+ 提交于 2019-12-08 03:56:23

问题


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.


回答1:


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();


来源:https://stackoverflow.com/questions/34642308/how-to-show-new-line-in-a-gridview-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!