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

[Solved] Text Format in radgrid

4 Answers 610 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anu
Top achievements
Rank 1
Anu asked on 28 Jan 2010, 11:49 AM
Hi,

I am trying to populate a radgrid from the data I stored in a backend database, which has a text field. The text field has contents added through a multi-line text box in more than one paragraph. However, when the data is displayed in the grid, the paragraphs are all merged together and the data is displayed as a single paragraph. Is there anything I can do to maintain this while displaying the data in radgrid.

many thanks,

Anu

4 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 28 Jan 2010, 12:06 PM
Hi Anu,

Browsers normally ignore white spaces in the HTML source, that's why you don't see them. You should either modify the strings before binding the RadGrid control, or modify them in the RadGrid ItemDataBound event, or set DataFormatString="<pre>{0}</pre>" for the respective column. You may need to add some font styles for the <pre> element so that it looks like the rest of the RadGrid content.

Here is how you can access a particular cell in ItemDataBound:
http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

All the best,
Dimo
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Anu
Top achievements
Rank 1
answered on 28 Jan 2010, 12:21 PM
Hi Dimo,

Adding that dataformat helped me to seperate them in paragraphs. However, the width of column has now grown to take the length of string of a single paragraph. Could you please explain me what kind of styles I should be applying to the <pre> element so that I can see the text in a normal way.

regards,

Anu
0
Anu
Top achievements
Rank 1
answered on 01 Feb 2010, 01:30 PM
Hi,

Can some one help me on this please? I have been applying various styles to add the linebreaks in the text. The latest problem is that, the text appears as i want in my local machine. However, when I deploy it to the server and access it, the width of the text box becomes half and the other half is not visible, thus losing a lot of text.

Any idea why this is happening?

Anu
0
Dimo
Telerik team
answered on 03 Feb 2010, 08:43 AM
Hello Anu,

Which textbox are you referring to?

By default, text inside a <pre> tag is wrapped only where there is a new line. If the text has new lines at too few places, you will have to use ItemDataBound to insert <br /> tags at the places where you want new lines (paragraphs) to appear in the browser.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<script runat="server">
 
    protected void RadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        DataRow dr;
        int colsNum = 1;
        int rowsNum = 2;
        string colName = "Column";
 
        for (int j = 1; j <= colsNum; j++)
        {
            dt.Columns.Add(String.Format("{0}{1}", colName, j));
        }
 
        for (int i = 1; i <= rowsNum; i++)
        {
            dr = dt.NewRow();
 
            for (int k = 1; k <= colsNum; k++)
            {
                dr[String.Format("{0}{1}", colName, k)] = String.Format(
@"{0}{1} {0}{1} {0}{1} {0}{1}
 
Row{2} Row{2} Row{2} Row{2}", colName, k, i);
            }
            dt.Rows.Add(dr);
        }
 
        (sender as RadGrid).DataSource = dt;
    }
 
    protected void RadGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem && !e.Item.IsInEditMode)
        {
            TableCell cell = (e.Item as GridDataItem)["Column1"];
            cell.Text = cell.Text.Replace("\n", "<br />");
        }
    }
     
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
<head runat="server">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>RadControls</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
 
<telerik:RadGrid
    ID="RadGrid1"
    runat="server"
    Width="200px"
    AutoGenerateColumns="false"
    OnNeedDataSource="RadGrid_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Column1" HeaderText="Column" DataFormatString="<pre>{0}</pre>" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
<br /><br />
 
<telerik:RadGrid
    ID="RadGrid2"
    runat="server"
    Width="200px"
    AutoGenerateColumns="false"
    OnNeedDataSource="RadGrid_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Column1" HeaderText="Column" DataFormatString="<pre>{0}</pre>" />
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <Scrolling AllowScroll="true" UseStaticHeaders="true" />
    </ClientSettings>
</telerik:RadGrid>
 
<br /><br />
 
<telerik:RadGrid
    ID="RadGrid3"
    runat="server"
    Width="200px"
    AutoGenerateColumns="false"
    OnItemDataBound="RadGrid_ItemDataBound"
    OnNeedDataSource="RadGrid_NeedDataSource">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Column1" HeaderText="Column" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 
</form>
</body>
</html>


Regards,
Dimo
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
Tags
Grid
Asked by
Anu
Top achievements
Rank 1
Answers by
Dimo
Telerik team
Anu
Top achievements
Rank 1
Share this question
or