Gridview中的string.replace数据绑定列

时间:2012-10-26 17:44:39

标签: c# gridview boundfield

我正在将数据库中的字符串注释拉到gridview中以显示给用户。在整个字符串中,我已将<br/>放置在我需要换行符的位置,但我想知道如何用“Environment.Newline”替换<br/> s。该列只是一个边界域。

  <Columns>  
            <asp:BoundField HeaderText="Comment" DataField="UAComment" />
  </Columns>

并使用适配器和Fill()填充表:

            adapter = new SqlDataAdapter(queryString, connection);
            connection.Open();
            adapter.SelectCommand = command;
            recordsFound = adapter.Fill(table);
            results.Text = recordsFound + " records matching query";
            connection.Close();

感谢您的任何信息。

ps:还尝试使用换行符构建字符串/将字符串提交到数据库,但似乎无法通过正确的格式将数据库拉出数据库。

public void Group(String message)
    {
        log.Append(": Group :");
        log.AppendFormat(message);
        log.Append("<br/>");
    }


    public String GetLog()
    {
        log.Replace("<br/>", Environment.NewLine);
        return log.ToString();
    }

还用@“\ n”,“\ n”,@“\ r \ n”,“\ r”代替“Environment.Newline”

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是使用模板字段而不是绑定字段,并使用当前列值添加模板字段中的文字绑定。使用它,它将以html格式呈现所有内容,并且自动换行。

示例:

<asp:TemplateField>
    <HeaderTemplate>
        Comment
    </HeaderTemplate>
    <ItemTemplate>
        <asp:Literal runat="server" ID="Literal1" Text='<%# Eval("UAComment") %>' />
    </ItemTemplate>
</asp:TemplateField>

希望这能解决您的问题

相关问题