This is a migrated thread and some comments may be shown as answers.

How to enter line breaks in RadGrid

5 Answers 2185 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randall
Top achievements
Rank 2
Randall asked on 09 Sep 2008, 03:21 PM
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

5 Answers, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 11 Sep 2008, 02:47 PM
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.
0
Randall
Top achievements
Rank 2
answered on 22 Oct 2008, 08:16 PM
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
0
Priyanka Agarwal
Top achievements
Rank 1
answered on 04 Jan 2010, 07:22 AM
Is there a sample code that you can share for reference
0
Mike
Top achievements
Rank 1
answered on 02 Jun 2011, 09:09 PM
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/>");
        }
    }
0
Randall
Top achievements
Rank 2
answered on 30 Jun 2011, 03:41 PM

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

Tags
Grid
Asked by
Randall
Top achievements
Rank 2
Answers by
Veli
Telerik team
Randall
Top achievements
Rank 2
Priyanka Agarwal
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Share this question
or