Search code examples
c#asp.nethtmlradgriddataformat

How to show raw XML string in Telerik:RadGrid columns without rendering


I have a telerik:GridBoundColumn in my telerik:RadGrid as a string type with some XML codes.

How can I show that column (raw XML) in that RadGrid without rendering?

My problems: my grid direction is right to left for some reason...
so at first I should change the xml direction like below :

<telerik:GridBoundColumn DataField="Settings" FilterControlAltText="Filter Settings column" DataFormatString="<span style='direction:ltr;'>{0}</span>"
    HeaderText="Settings" SortExpression="Settings" 
    UniqueName="Settings" FilterImageToolTip="Filter" HtmlEncode="false">
    <HeaderStyle HorizontalAlign="Right" VerticalAlign="Middle" />
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</telerik:GridBoundColumn>

Currently we have an XML string with ltr direction in grid. After that I change to HtmlEncode="TRUE", but by doing so I lose the ltr direction. so I set HtmlEncode back to false.

I figured out I can use <xmp> or <pre> elements. But with <xmp> I have some replacement of < and > in grid and also it has been deprecated... and with <pre> I should change all < to &lt; and > to &gt; in my database, which is unfeasible.


Solution

  • Add handler for onItemDataBound for your Grid

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True"
                AllowSorting="True" PageSize="50" ShowFooter="True" AllowPaging="True" 
                AutoGenerateColumns="False" GridLines="None" ShowStatusBar="true" 
                    onitemdatabound="RadGrid1_ItemDataBound">
    

    And codebehind:

     protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
     {
                if (e.Item is GridDataItem)
                {
                    GridDataItem item = e.Item as GridDataItem;
                    item["XmlColumn1"].Text = Server.HtmlEncode(item["XmlColumn1"].Text);
                    item["XmlColumn2"].Text = Server.HtmlEncode(item["XmlColumn2"].Text);
                }
    
      }
    

    Should do the trick.