How to enter line breaks in RadGrid

6 posts, 1 answers
  1. Randall
    Randall avatar
    54 posts
    Member since:
    Jun 2008

    Posted 09 Sep 2008 Link to this post

    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

  2. Answer
    Veli
    Admin
    Veli avatar
    2002 posts
    Member since:
    Sep 2012

    Posted 11 Sep 2008 Link to this post

    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.

  3. DevCraft Release
  4. Randall
    Randall avatar
    54 posts
    Member since:
    Jun 2008

    Posted 22 Oct 2008 Link to this post

    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

  5. Priyanka Agarwal
    Priyanka Agarwal avatar
    64 posts
    Member since:
    Oct 2009

    Posted 04 Jan 2010 Link to this post

    Is there a sample code that you can share for reference

  6. Mike
    Mike avatar
    6 posts
    Member since:
    Jun 2009

    Posted 02 Jun 2011 Link to this post

    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/>");
            }
        }

  7. Randall
    Randall avatar
    54 posts
    Member since:
    Jun 2008

    Posted 30 Jun 2011 Link to this post

    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

Back to Top
DevCraft Release