Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Grid > How to enter line breaks in RadGrid

Answered How to enter line breaks in RadGrid

Feed from this thread
  • Posted on Sep 9, 2008 (permalink)

    Hello,

    I am using RadGrid from RadControls for ASP.NET v2008.1.619 to display and edit TEXT fields from a Microsoft SQL Server database.

    I am trying to insert line breaks in the text using an ASP.NET TextBox (multi-line) in an EditFormTemplate.  When I press Enter the line break works as expected.  When I update the RadGrid the text in the RadGrid is not honoring the line break.  I also tried using Shift-Enter which works in the ASP.NET TextBox but when displayed back in the Grid after updating does not work.  I tried using <br> tags but they just render in grid as <br> and do not break the line.

    Is there a way to enter line breaks and have them work in the RadGrid?

    Thanks,

    Randall Price

    Reply

  • Answer Veli Veli admin's avatar

    Posted on Sep 11, 2008 (permalink)

    Hi Randall,

    Please note that a TextBox control returns a space-separated string out of multiple lines on the server. Therefore, RadGrid only reads a string with characters and spaces inside, and no line breaks.

    Additionally, line-feed is achieved in various ways for various operation platforms (<br /> for html, "\n" and "\r\n" for code, &#10 for Excel, etc.), therefore, even if you modify your string before comitting to the database, it will not be consistently displayed everywhere.

    As for RadGrid, the only way of explicitly returning to a new line is thorugh the break tag (<br />), as cell values are eventually rendered as HTML text.

    Sincerely yours,
    Veli
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Posted on Oct 22, 2008 (permalink)

    Hello Veli,

    I just wanted to say that I have this working well, per your suggestions.  In the RadEditor, I set NewLineBr="True" which inserts <br /> tags into the text as the user presses the Enter key.  I translate these tags into CrLf when saving the RadEditor contents to the TEXT data type field in SQL Server.  Then when I read this field, I do the reverse and translate the CrLf back into <br /> tags for display and editing.  Works like a charm!

    This even works fine for when I use the Telerik Reporting control to create an on-screen report from the data, and also when I use the Telerik Reporting controls' Export feature to export to .RTF and/or .PDF files.  Nice!

    Thanks for your help,

    Randall Price

    Reply

  • Priyanka Agarwal avatar

    Posted on Jan 4, 2010 (permalink)

    Is there a sample code that you can share for reference

    Reply

  • Mike avatar

    Posted on Jun 2, 2011 (permalink)

    protected void Grid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                item["colname"].Text = item["colname"].Text.Replace("\n", "<br/>");
            }
        }

    Reply

  • Posted on Jun 30, 2011 (permalink)

    Hello Priyanka,

    I convert the input for the RadEditor (i.e., "\n" becomes "<br />") during the ItemDataBound() event for the RadGrid when loading the data.  Then when I update the grid, I convert the RadEditor content back (i.e., "\n\r" becomes "\n") during the UpdateCommand() of the RadGrid.  It took me a little bit of trial and error to discover exactly what text characters to replace, but these are working for my situation.

    Here are my conversion functions:

    private string _FormatRadEditorContentInput(string strText)
    {
        // Replace Line Feed with HTML line break.
        string strResult = strText.Replace("\n", "<br />");
      
        return strResult;
    }
    private string _FormatRadEditorContentOutput(string strText)
    {
        // Replace Line Feed / Carriage Return with just a Line Feed.
        string strResult = strText.Replace("\n\r ", "\n");
      
        return strResult;
    }

    So during ItemDataBound() event, convert the data coming in as follows:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem oItem = (GridDataItem)e.Item; 
            oItem["Comments"].Text = _FormatRadEditorContentInput(oItem["Comments"].Text); 
        
    }

    During the UpdateCommand() event, convert the data as it is being saved as follows:

    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        GridEditableItem oEditedItem = e.Item as GridEditableItem;
      
        string        strComments   = (oEditedItem.FindControl("radEditor_Comments") as RadEditor).Text;
        string        strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(strConnString);
      
        SqlCommand sqlCommand  = new SqlCommand();
        sqlCommand.CommandText = "UpdateTheTable";
        sqlCommand.CommandType = CommandType.StoredProcedure;
        sqlCommand.Connection  = sqlConnection;
      
        sqlCommand.Parameters.AddWithValue("@strComments", _FormatRadEditorContentOutput(strComments));
      
        try
        {
            sqlConnection.Open();
            sqlCommand.ExecuteNonQuery();
        }
        finally
        {
            sqlConnection.Close();
        }
    }

    I hope this helps you out.

    Thanks,

    Randall Price
    Senior Developer
    Virginia Tech

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > Grid > How to enter line breaks in RadGrid
Related resources for "How to enter line breaks in RadGrid"

ASP.NET Grid Features  |  Documentation  |  Demos  |  Step-by-step Tutorial  ]