【问题标题】:LineBreak not working in Binding of GridViewLineBreak 在 GridView 的绑定中不起作用
【发布时间】:2015-07-31 10:36:24
【问题描述】:

在 ASP.NET 应用程序中,我有如下所列的多行字符串值。

代码:

string remarks="Line 1 \n";
remarks+=remarks+"\n Line 2 \n";

Console.Writeln(remarks);

输出:

Line 1

Line 2

现在我想要字符串值的相同输出,当我将它们插入到 SQL Server 上的表中时,检索它们,然后通过绑定将它们呈现在 GridView 中。

但是在从数据库中检索之后,我只能像这样将整个字符串放在一行中(换行符不起作用):

Line 1Line 2

我尝试了Environement.NewLineStringBuilder,但我总是以单线方式从服务器获取输出。

我做错了什么?

【问题讨论】:

  • 你是如何将它插入数据库的?你是如何找回它的?
  • 将 Ado.NET 与存储过程结合使用。

标签: c# asp.net string sql-server-2008 line-breaks


【解决方案1】:

在行绑定事件中将“\n”替换为“

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

【讨论】:

    【解决方案2】:

    SQL 将保留插入的换行符。

    默认情况下,换行符不会显示在 DataGridView 对象上。 试试:

    dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 2012-09-02
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多